Maximum Number of Words Found in Sentences

Easy
660.

Maximum Number of Words Found in Sentences

Maximum Number of Words Found in Sentences is an easy coding problem on arrays and strings, asked in TCS, Accenture and Capgemini interviews.

A sentence is a list of words separated by single spaces, with no leading or trailing spaces.

Given an array sentences, return the maximum number of words that appear in any single sentence.

Example 1

Input
sentences = ["alice and bob love codekairo","i think so too","this is great thanks very much"]
Output
6
Explanation
The last sentence has six words.

Example 2

Input
sentences = ["please wait","continue to fight","continue to win"]
Output
3

Example 3

Input
sentences = ["solo"]
Output
1

Constraints

  • 1 <= sentences.length <= 100
  • 1 <= sentences[i].length <= 100
  • Words are separated by single spaces with no leading or trailing space.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string[]} sentences
 * @return {number}
 */
var mostWordsFound = function(sentences) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Maximum Number of Words Found in Sentences — Solution

Maximum Number of Words Found in Sentences solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Maximum Number of Words Found in Sentences is an easy coding problem on arrays and strings, asked in TCS, Accenture and Capgemini interviews.

  • Difficulty: Easy
  • Topics: Arrays, Strings
  • Asked at: TCS, Accenture, Capgemini
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

A sentence is a list of words separated by single spaces, with no leading or trailing spaces.

Given an array sentences, return the maximum number of words that appear in any single sentence.

Example 1

Input: sentences = ["alice and bob love codekairo","i think so too","this is great thanks very much"]
Output: 6
Explanation: The last sentence has six words.

Example 2

Input: sentences = ["please wait","continue to fight","continue to win"]
Output: 3

Example 3

Input: sentences = ["solo"]
Output: 1

Constraints

  • 1 <= sentences.length <= 100
  • 1 <= sentences[i].length <= 100
  • Words are separated by single spaces with no leading or trailing space.

Hints for Maximum Number of Words Found in Sentences

  1. Hint 1

    With single spaces and no padding, the word count is the space count plus one.

  2. Hint 2

    No splitting is needed at all — just count the spaces.

  3. Hint 3

    Track the running maximum as you go.

How to solve Maximum Number of Words Found in Sentences

The formatting guarantee turns counting words into counting separators: n single spaces split a sentence into exactly n + 1 words.

Approach

  1. For each sentence, count the space characters.
  2. The word count is that plus one.
  3. Keep the largest value seen.

Why it works

Because there are no leading, trailing or repeated spaces, every space sits strictly between two words, so the separators and the gaps between words are in bijection.

Complexity

  • Time — O(total characters)
  • Space — O(1)

Pitfalls

  • Splitting on whitespace in a language that produces an empty leading token would be off by one on padded input — the guarantee is what makes the shortcut safe.
  • Starting the count at 0 rather than 1 undercounts every sentence.

Reference solution

Python

from typing import List

def mostWordsFound(sentences: List[str]) -> int:
    return max(s.count(" ") + 1 for s in sentences)

Java

public static int mostWordsFound(String[] sentences) {
    int best = 0;
    for (String s : sentences) {
        int words = 1;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') words++;
        }
        if (words > best) best = words;
    }
    return best;
}

C++

int mostWordsFound(vector<string>& sentences) {
    int best = 0;
    for (const string& s : sentences) {
        int words = 1;
        for (char c : s) {
            if (c == ' ') words++;
        }
        if (words > best) best = words;
    }
    return best;
}

JavaScript

var mostWordsFound = function(sentences) {
    var best = 0;
    for (var j = 0; j < sentences.length; j++) {
        var s = sentences[j], words = 1;
        for (var i = 0; i < s.length; i++) {
            if (s.charAt(i) === " ") words++;
        }
        if (words > best) best = words;
    }
    return best;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Strings