One The sentence By some word And a single space between them , There will be no extra spaces at the beginning and end of the sentence .
Here's an array of strings sentences
, among sentences[i] Represents a single The sentence .
Please return to a single sentence The maximum number of words .
Input :
sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]
Output :
6
explain :
- The first sentence "alice and bob love leetcode" All in all 5 Word .
- The second sentence "i think so too" All in all 4 Word .
- The third sentence "this is great thanks very much" All in all 6 Word .
therefore , The third sentence has the largest number of words in a single sentence , All in all 6 Word .
Input :
sentences = ["please wait", "continue to fight", "continue to win"]
Output :
3
explain :
There may be more than one sentence with the same number of words .
In this case , The second sentence and the third sentence ( Bold italics ) There are the same number of words .
1 <= sentences.length <= 100
1 <= sentences[i].length <= 100
sentences[i]
Only lowercase letters and ' '
.sentences[i]
There are no spaces at the beginning and end of .sentences[i]
All words in are separated by a single space .class Solution {
public int mostWordsFound(String[] sentences) {
int ans = 0;
for (String s : sentences) {
int count = 1;
for (char c : s.toCharArray()) {
if (c == ' ') {
count++;
}
}
if (count > ans) {
ans = count;
}
}
return ans;
}
}
int mostWordsFound(char ** sentences, int sentencesSize){
int ans = 0;
for (int i = 0; i < sentencesSize; i++) {
int count = 1;
char *s = sentences[i];
while (*s++) {
if (*s == ' ') {
count++;
}
}
if (count > ans) {
ans = count;
}
}
return ans;
}
class Solution {
public:
int mostWordsFound(vector<string>& sentences) {
int ans = 0;
for (string &s: sentences) {
int cnt = count(s.begin(), s.end(), ' ') + 1;
if (cnt > ans) {
ans = cnt;
}
}
return ans;
}
};
class Solution:
def mostWordsFound(self, sentences: List[str]) -> int:
ans = 0
for sentence in sentences:
cnt = sentence.count(' ') + 1
if cnt > ans:
ans = cnt
return ans
func mostWordsFound(sentences []string) int {
ans := 0
for _, s := range sentences {
cnt := 1
for _, c := range s {
if c == ' ' {
cnt++
}
}
if cnt > ans {
ans = cnt
}
}
return ans
}
impl Solution {
pub fn most_words_found(sentences: Vec<String>) -> i32 {
let mut ans = 0;
sentences.iter().for_each(|s| {
let cnt = s.as_bytes().iter().filter(|&&c| {
c as char == ' ' }).count() + 1;
if cnt > ans {
ans = cnt;
}
});
return ans as i32;
}
}
Thank you very much for reading this article ~
welcome 【 give the thumbs-up 】【 Collection 】【 Comment on 】~
It's not hard to give up , But persistence must be cool ~
I hope all of us can make a little progress every day ~
This paper is written by The white hat of the second leader :https://le-yi.blog.csdn.net/ Original blog ~