findLongestWord

Easy · String Manipulations · 2 test cases

Return the length of the longest word in the provided sentence.

Examples

findLongestWord('The quick brown fox jumped over the lazy dog') → 6

Starter code

function findLongestWord(str) {

}

Complexity of the optimal solution

Time O(n), space O(n). The string is first split into an array of words (O(n) time, O(n) space). Then, it maps over the words to get their lengths (O(m) where m is number of words). `Math.max` also takes O(m). The dominant factor is the initial split, making it O(n) overall.

Solve findLongestWord in the browser