Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 616 Bytes

File metadata and controls

28 lines (23 loc) · 616 Bytes

LeetCode Records - Question 1816 Truncate Sentence

Attempt 1:

class Solution {
    public String truncateSentence(String s, int k) {
        StringBuilder stringBuilder = new StringBuilder();
        int count = 0;

        for (char ch : s.toCharArray()) {
            if (ch == ' ') {
                count++;
                if (count == k) {
                    break;
                }
            }

            stringBuilder.append(ch);
        }

        return stringBuilder.toString();
    }
}
  • Runtime: 1 ms (Beats: 74.72%)
  • Memory: 41.66 MB (Beats: 60.86%)