Top N Frequent Words
Write a function topNFrequentWords that takes a large text document as input and returns the top N most frequent words. The function should be optimized for performance.
Function Signature
function topNFrequentWords(text, N);
Example 1:
Input:
text = "the quick brown fox jumps over the lazy dog the the the quick quick",N = 2
Output:["the", "quick"]
Explanation: "the" and "quick" are the most frequent words appearing 4 and 3 times respectively.
Example 2:
Input:
text = "a a a b b c",N = 1
Output:["a"]
Explanation: "a" is the most frequent word appearing 3 times.
Example 3:
Input:
text = "apple banana apple banana orange apple banana banana",N = 2
Output:["banana", "apple"]
Explanation: "banana" and "apple" are the most frequent words appearing 4 and 3 times respectively.
Solve this problem
Start your 7-day free trial to solve this problem in our code editor with instant feedback.
Start 7-Day Free Trial to Solve This ProblemRelated Problems
Longest Palindromic Substring
hardWrite a function `longestPalindrome` that takes a string and returns the longest palindromic substring. ## Function Signature ```function longestPalindrome(s);``` ### Example 1: > **Input:** `s = "babad"` \ > **Output:** `"bab"` \ > **Explanation:** The longest palindromic substring is "bab". ### Example 2: > **Input:** `s = "cbbd"` \ > **Output:** `"bb"` \ > **Explanation:** The longest palindromic substring is "bb". ### Example 3: > **Input:** `s = "a"` \ > **Output:** `"a"` \ > **Explanation:** The longest palindromic substring is "a".
StringWord Break
hardWrite a function `wordBreak` that takes a string s and a dictionary of strings wordDict, and returns true if s can be segmented into a space-separated sequence of one or more dictionary words. ## Function Signature ```function wordBreak(s, wordDict);``` ### Example 1: > **Input:** `s = "bigdevsoon"`, `wordDict = ["big","dev","soon"]` \ > **Output:** `true` \ > **Explanation:** The string "bigdevsoon" can be segmented as "big dev soon". ### Example 2: > **Input:** `s = "applepenapple"`, `wordDict = ["apple","pen"]` \ > **Output:** `true` \ > **Explanation:** The string "applepenapple" can be segmented as "apple pen apple". ### Example 3: > **Input:** `s = "catsandog"`, `wordDict = ["cats","dog","sand","and","cat"]` \ > **Output:** `false` \ > **Explanation:** The string "catsandog" cannot be segmented as a space-separated sequence of one or more dictionary words.
StringLongest Substring Without Repeating Characters
hardFind the length of the longest substring without repeating characters. ## Function Signature ```function lengthOfLongestSubstring(s);``` ### Example 1: > **Input:** `s = "abcabcbb"` \ > **Output:** `3` \ > **Explanation:** The longest substring without repeating characters is "abc". ### Example 2: > **Input:** `s = "bbbbb"` \ > **Output:** `1` \ > **Explanation:** The longest substring without repeating characters is "b" of length 1.
String