Published:
Updated:

https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/


Solution

class Solution {
    static int mostWordsFound(String[] sentences) {
        int max = Integer.MIN_VALUE;
        int cnt = 1;
        for (int i = 0; i < sentences.length; i++) {
            for (int j = 0; j < sentences[i].length(); j++) {
                char[] arrSentences = sentences[i].toCharArray();
                if (arrSentences[j] == ' ') {
                    cnt++;
                }

                if (cnt > max) {
                    max = cnt;
                }
            }

            cnt = 1;
        }

        return max;
    }

    public static void main(String[] args) {
        System.out.print(mostWordsFound(new String[]{"alice and bob love leetcode", "i think so too", "this is great thanks very much"}));
    }
}

Leave a comment