Overview
There is a group of sentences given. Find the maximum number of words that appear in a sentence within that group of sentences
Example
Input: ["Hello World", "This is hat]
Output: 3
Input: ["Hello World", "This is hat", "The cat is brown"]
Output: 4
Program
Here is the program for the same.
package main
import "fmt"
func mostWordsFound(sentences []string) int {
lenSentences := len(sentences)
max := 0
for i := 0; i < lenSentences; i++ {
countWord := countW(sentences[i])
if countWord > max {
max = countWord
}
}
return max
}
func countW(s string) int {
lenS := len(s)
numWords := 0
for i := 0; i < lenS; {
for i < lenS && string(s[i]) == " " {
i++
}
if i < lenS {
numWords++
}
for i < lenS && string(s[i]) != " " {
i++
}
}
return numWords
}
func main() {
output := mostWordsFound([]string{"Hello World", "This is hat"})
fmt.Println(output)
output = mostWordsFound([]string{"Hello World", "This is hat", "The cat is brown"})
fmt.Println(output)
}
Output
3
4