K most occurring strings
Last Updated :
21 Feb, 2023
Given an array arr[] of N strings and an integer K, the task is to print K strings which occurred the most number of times in the arr[]. If two or more strings have the same frequency then print the lexicographically smallest string.
Note: The value of K is always less than or equal to the number of distinct elements in the array.
Examples:
Input: str[] = {“geeks”, “geeksforgeeks”, “geeks”, “article”}, K = 1
Output: “geeks”
Explanation:
“geeks” –> 2
“geeksforgeeks” –> 1
“article” –> 1
Hence, the most occurring string is “geeks”
Input: str[] = {“car”, “bus”, “car”, “bike”, “car”, “bus”, “bike”, “cycle”}, K = 2
Output : “car”, “bus”
Explanation:
“car” –> 3
“bus” –> 2
“bike” –> 2
“cycle” –> 1
string “car” has highest frequency, string “bus” and “bike” both have second highest frequency, but string “bus” is lexicographically small because it has shorter length.
Approach:
- Count the frequency of each string in the array and store it in a HashMap where the string is the key and frequency as the value.
- Now, sort these keys according to their frequencies in ascending order, this is done to keep keys with the least frequencies at the top.
- The strings with equal frequencies are prioritized alphabetically, i.e., the string which is alphabetically greater has a higher priority.
- Delete the top N – K key-value pairs from the HashMap. By doing this the container is left with K keys with the highest frequencies, in reverse order.
- Print the strings stored in the HashMap.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
map<string, int > Freq;
class compare {
public :
bool operator()(string& a, string& b)
{
if (a == b) {
return Freq[a] > Freq[b];
}
return a > b;
}
};
vector<string> frequentWords(vector<string> arr, int K)
{
map<string, int > Freq;
for ( auto word : arr) {
Freq[word]++;
}
priority_queue<string, vector<string>, compare> Order;
for ( auto it : Freq) {
Order.push(it.first);
if (Order.size() > K) {
Order.pop();
}
}
vector<string> ans;
while (Order.size()) {
ans.push_back(Order.top());
Order.pop();
}
reverse(ans.begin(), ans.end());
return ans;
}
int main()
{
int N = 3, K = 2;
vector<string> arr{ "car" , "bus" , "car" };
vector<string> ans = frequentWords(arr, K);
for ( auto it : ans) {
cout << it << endl;
}
}
|
Java
import java.util.*;
class FrequentWords {
public static ArrayList<String>
frequentWords(ArrayList<String> arr, int K)
{
HashMap<String, Integer> Freq
= new HashMap<>();
for (String word : arr) {
Freq.put(word,
Freq.getOrDefault(word, 0 )
+ 1 );
}
PriorityQueue<String> Order
= new PriorityQueue<>(
(a, b)
-> (Freq.get(a) != Freq.get(b))
? Freq.get(a) - Freq.get(b)
: b.compareTo(a));
for (String word : Freq.keySet()) {
Order.offer(word);
if (Order.size() > K) {
Order.poll();
}
}
ArrayList<String> ans
= new ArrayList<>();
while (!Order.isEmpty()) {
ans.add(Order.poll());
}
Collections.reverse(ans);
return ans;
}
public static void
main(String[] args)
{
int N = 3 , K = 2 ;
ArrayList<String> arr
= new ArrayList<String>();
arr.add( "car" );
arr.add( "bus" );
arr.add( "car" );
ArrayList<String> ans
= frequentWords(arr, K);
for (String word : ans) {
System.out.println(word + " " );
}
}
}
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Solution
{
public static IList< string > frequentWords(IList< string > arr, int K)
{
Dictionary< string , int > Freq = new Dictionary< string , int >();
foreach ( string word in arr)
{
if (Freq.ContainsKey(word))
{
Freq[word]++;
}
else
{
Freq[word] = 1;
}
}
SortedSet< string > Order = new SortedSet< string >(Comparer< string >.Create((a, b) =>
{
int freqCompare = Freq[b].CompareTo(Freq[a]);
if (freqCompare == 0)
{
return string .Compare(a, b, StringComparison.Ordinal);
}
return freqCompare;
}));
foreach ( string word in Freq.Keys)
{
Order.Add(word);
if (Order.Count > K)
{
Order.Remove(Order.Last());
}
}
return Order.ToList();
}
public static void Main( string [] args)
{
int N = 3, K = 2;
List< string > arr = new List< string >() { "car" , "bus" , "car" };
List< string > ans = frequentWords(arr, K).ToList();
foreach ( string word in ans)
{
Console.WriteLine(word);
}
}
}
|
Javascript
function frequentWords(arr, K)
{
var Freq = new Map();
for ( var word of arr) {
Freq.set(word, Freq.has(word)?Freq.get(word):0);
}
var Order = [];
for ( var word of [...Freq.keys()]) {
Order.push(word);
if (Order.length > K) {
Order.pop();
}
}
var ans = [];
Order.sort((a,b)=>a[0]-b[0]);
while (Order.length!=0) {
ans.push(Order[Order.length-1])
Order.pop();
}
ans.reverse();
return ans;
}
var N = 3, K = 2;
var arr = [];
arr.push( "car" );
arr.push( "bus" );
arr.push( "car" );
var ans = frequentWords(arr, K);
for ( var word of ans) {
console.log(word);
}
|
Python3
import collections
from collections import Counter
from queue import PriorityQueue
def frequentWords(arr, K):
Freq = Counter(arr)
Order = PriorityQueue()
for word in Freq:
Order.put(( - Freq[word], word))
if Order.qsize() > K:
Order.get()
ans = []
while not Order.empty():
ans.append(Order.get()[ 1 ])
ans.reverse()
return ans
if __name__ = = "__main__" :
N = 3
K = 2
arr = [ "car" , "bus" , "car" ]
ans = frequentWords(arr, K)[:: - 1 ]
print ( * ans, sep = "\n" )
|
Time Complexity: O(N*log2N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...