Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You are given a list of words present in a book. Your younger brother is really curious to know the K most frequent words in the book, you have to find them.
- Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order should come first.
- Input format
- There are three lines of input
- The first line contains N, which is the number of input strings.
- The second line contains N space separated input strings (S).
- The third line contains the value of K.
- Output format
- Print the K most frequent words present inside the book, each in a new line. If two words have the same frequency, then the word with the lower alphabetical order should come first.
- Constraints
- 1 <= N <= 1000
- 1 <= Length(S) <= 100000
- 'a' <= S[i] <= 'z'
- 1 <= K <= Number of Unique Strings
- Sample Input 1
- 1
- bus
- 1
- Sample Output 1
- bus
- Explanation 1
- In the given input, the frequency of "bus" is 1 and it is the 1st most frequent word.
- Sample Input 2
- 3
- car bus car
- 2
- Sample Output 2
- car
- bus
- Explanation 2
- In the given input, the frequency of "car" is 2, frequency of “bus” is 1.
- Since K = 2, both words are printed, with "car" being the most frequent, gets printed first.
- */
- /**
- * @param {string[]} n
- * @param {number} k
- * @return {string[]}
- */
- function frequentWords(words, k) {
- const mp= new Map();
- const len=words.length;
- words.map(word=>{
- if(mp.get(word)){
- mp.set(word,mp.get(word)+1);
- }else{
- mp.set(word,1);
- }
- })
- // map<word,itsFrequency>
- let arr= new Array();
- // arr=[{fre,word},{},{},...]
- // console.log(mp)
- mp.forEach((value,key)=>{
- arr.push({fre:value,word:key});
- });
- // console.log(arr);
- arr=arr.sort((a,b)=>{
- const fre1=a.fre;
- const fre2=b.fre;
- const word1=a.word;
- const word2=b.word;
- if(fre1==fre2){
- return word1.localeCompare(word2);
- }
- return fre2-fre1;
- })
- const ans= arr.slice(0,k).map(ele=>ele.word);
- return ans;
- }
- function main() {
- const n = parseInt(readLine())
- const words = readLine().split(' ')
- const k = parseInt(readLine())
- const result = frequentWords(words, k)
- console.log(result.join("\n"))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement