程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[algorithm] 2114 Maximum number of words in a sentence (Java / C / c++ / Python / go / trust)

編輯:Python

List of articles

  • 2114. Maximum number of words in a sentence :
  • Examples 1:
  • Examples 2:
  • Tips :
  • analysis
  • Answer key
    • java
    • c
    • c++
    • python
    • go
    • rust
  • Original title transmission gate :https://leetcode-cn.com/problems/maximum-number-of-words-found-in-sentences/


2114. Maximum number of words in a sentence :

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 .

Examples 1:

 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 .

Examples 2:

 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 .

Tips :

  • 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 .

analysis

  • Facing this algorithm problem , The second leader was lost in thought .
  • According to the prompt , There are spaces between each word , And there are no spaces at the beginning and end , Then the number of words is the number of spaces plus one .

Answer key

java

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;
}
}

c

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;
}

c++

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;
}
};

python

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

go

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
}

rust

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;
}
}


Original title transmission gate :https://leetcode-cn.com/problems/maximum-number-of-words-found-in-sentences/


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 ~



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved