Skip to content

Commit 7320d8c

Browse files
solves #2114: MaximumNumberOfWordsFoundInSentences in java
1 parent 5b382a5 commit 7320d8c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@
701701
| 2099 | [Find Subsequence of Length K With the Largest Sum](https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum) | [![Java](assets/java.png)](src/FindSubsequenceOfLengthKWithTheLargestSum.java) | |
702702
| 2103 | [Rings and Rods](https://leetcode.com/problems/rings-and-rods) | [![Java](assets/java.png)](src/RingsAndRods.java) | |
703703
| 2108 | [Find First Palindromic String in the Array](https://leetcode.com/problems/find-first-palindromic-string-in-the-array) | [![Java](assets/java.png)](src/FindFirstPalindromicStringInArray.java) | |
704-
| 2114 | [Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences) | | |
704+
| 2114 | [Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences) | [![Java](assets/java.png)](src/MaximumNumberOfWordsFoundInSentences.java) | |
705705
| 2119 | [A Number After a Double Reversal](https://leetcode.com/problems/a-number-after-a-double-reversal) | | |
706706
| 2124 | [Check if All A's Appears Before All B's](https://leetcode.com/problems/check-if-all-as-appears-before-all-bs) | | |
707707
| 2129 | [Capitalize the Title](https://leetcode.com/problems/capitalize-the-title) | | |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://leetcode.com/problems/maximum-number-of-words-found-in-sentences
2+
// T: O(s * |s|)
3+
// S: O(1)
4+
// s = no. of sentences and |s| length of sentence
5+
6+
public class MaximumNumberOfWordsFoundInSentences {
7+
public int mostWordsFound(String[] sentences) {
8+
int maxWords = 0;
9+
for (String sentence : sentences) {
10+
maxWords = Math.max(maxWords, totalWords(sentence));
11+
}
12+
return maxWords;
13+
}
14+
15+
private int totalWords(String string) {
16+
int count = 0;
17+
for (int i = 0 ; i < string.length() ; i++) {
18+
if (string.charAt(i) == ' ') count++;
19+
}
20+
return count + 1;
21+
}
22+
}

0 commit comments

Comments
 (0)