Given two string ‘S1’ and ‘S2’, the task is to return the most frequent (which is used the maximum number of times) word from ‘S1’ that is not present in ‘S2’. If more than one word is possible then print lexicographically smallest among them.
Examples:
Input: S1 = “geeks for geeks is best place to learn”, S2 = “bad place”
Output: geeks
“geeks” is the most frequent word in S1 and is also not present in S2.
The frequency of “geeks” is 2
Input: S1 = “the quick brown fox jumps over the lazy dog”, S2 = “the brown fox jumps”
Output: dog
All the words have frequency 1.
The lexicographically smallest word is “dog”
Approach:
The thought process must begin with the creation of a map to store key-value pair( string, int). Following this begins the extraction of the words from the first string while updating the map and the count. For every word from the second array that is present in the first array, reset the count. Finally, traverse the map and find the word with the highest frequency and get the lexicographically smallest one.
Algorithm:
- Iterate through string S2 and create a map and insert all of the words in it to the map.
- Iterate through string S1 and check whether the word is not present in the map created in the previous step or not.
- If the word satisfies the condition then update the answer if the frequency of the same word is maximum till now.
- If the frequency of the word is equal to the previously chosen word then update the answer according to lexicographically smallest of the two strings.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string smallestFreq(string S1, string S2)
{
map<string, int > banned;
for ( int i = 0; i < S2.length(); ++i) {
string s = "" ;
while (i < S2.length() && S2[i] != ' ' )
s += S2[i++];
banned[s]++;
}
map<string, int > result;
string ans;
int freq = 0;
for ( int i = 0; i < S1.length(); ++i) {
string s = "" ;
while (i < S1.length() && S1[i] != ' ' )
s += S1[i++];
if (banned[s] == 0) {
result[s]++;
if (result[s] > freq
|| (result[s] == freq && s < ans)) {
ans = s;
freq = result[s];
}
}
}
return ans;
}
int main()
{
string S1 = "geeks for geeks is best place to learn" ;
string S2 = "bad place" ;
cout << smallestFreq(S1, S2);
return 0;
}
|
Java
import java.util.HashMap;
class GFG
{
static String smallestFreq(String S1,
String S2)
{
HashMap<String,
Integer> banned = new HashMap<>();
for ( int i = 0 ; i < S2.length(); i++)
{
String s = "" ;
while (i < S2.length() &&
S2.charAt(i) != ' ' )
s += S2.charAt(i++);
banned.put(s, banned.get(s) == null ?
1 : banned.get(s) + 1 );
}
HashMap<String,
Integer> result = new HashMap<>();
String ans = "" ;
int freq = 0 ;
for ( int i = 0 ; i < S1.length(); i++)
{
String s = "" ;
while (i < S1.length() &&
S1.charAt(i) != ' ' )
s += S1.charAt(i++);
if (banned.get(s) == null )
{
result.put(s, result.get(s) == null ? 1 :
result.get(s) + 1 );
if (result.get(s) > freq ||
(result.get(s) == freq &&
s.compareTo(ans) < 0 ))
{
ans = s;
freq = result.get(s);
}
}
}
return ans;
}
public static void main(String[] args)
{
String S1 = "geeks for geeks is best place to learn" ;
String S2 = "bad place" ;
System.out.println(smallestFreq(S1, S2));
}
}
|
Python3
from collections import defaultdict
def smallestFreq(S1, S2):
banned = defaultdict( lambda : 0 )
i = 0
while i < len (S2):
s = ""
while i < len (S2) and S2[i] ! = ' ' :
s + = S2[i]
i + = 1
i + = 1
banned[s] + = 1
result = defaultdict( lambda : 0 )
ans = ""
freq = 0
i = 0
while i < len (S1):
s = ""
while i < len (S1) and S1[i] ! = ' ' :
s + = S1[i]
i + = 1
i + = 1
if banned[s] = = 0 :
result[s] + = 1
if (result[s] > freq or
(result[s] = = freq and s < ans)):
ans = s
freq = result[s]
return ans
if __name__ = = "__main__" :
S1 = "geeks for geeks is best place to learn"
S2 = "bad place"
print (smallestFreq(S1, S2))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static String smallestFreq(String S1,
String S2)
{
Dictionary<String,
int > banned = new Dictionary<String,
int >();
for ( int i = 0; i < S2.Length; i++)
{
String s = "" ;
while (i < S2.Length &&
S2[i] != ' ' )
s += S2[i++];
if (banned.ContainsKey(s))
{
var val = banned[s];
banned.Remove(s);
banned.Add(s, val + 1);
}
else
{
banned.Add(s, 1);
}
}
Dictionary<String,
int > result = new Dictionary<String,
int >();
String ans = "" ;
int freq = 0;
for ( int i = 0; i < S1.Length; i++)
{
String s = "" ;
while (i < S1.Length &&
S1[i] != ' ' )
s += S1[i++];
if (!banned.ContainsKey(s))
{
if (result.ContainsKey(s))
{
var val = result[s];
result.Remove(s);
result.Add(s, val + 1);
}
else
{
result.Add(s, 1);
}
if (result[s] > freq ||
(result[s] == freq &&
s.CompareTo(ans) < 0))
{
ans = s;
freq = result[s];
}
}
}
return ans;
}
public static void Main(String[] args)
{
String S1 = "geeks for geeks is best place to learn" ;
String S2 = "bad place" ;
Console.WriteLine(smallestFreq(S1, S2));
}
}
|
Javascript
<script>
function smallestFreq(S1,S2)
{
let banned = new Map();
for (let i = 0; i < S2.length; i++)
{
let s = "" ;
while (i < S2.length &&
S2[i] != ' ')
s += S2[i++];
banned.set(s, banned[s] == null ?
1 : banned.get(s) + 1);
}
let result = new Map();
let ans = "";
let freq = 0;
// find smallest and most frequent word
for (let i = 0; i < S1.length; i++)
{
let s = "";
while (i < S1.length &&
S1[i] != ' ')
s += S1[i++];
if (banned.get(s) == null )
{
result.set(s, result.get(s) == null ? 1 :
result.get(s) + 1);
if (result.get(s) > freq ||
(result.get(s) == freq &&
s < (ans) ))
{
ans = s;
freq = result.get(s);
}
}
}
return ans;
}
let S1 = "geeks for geeks is best place to learn" ;
let S2 = "bad place" ;
document.write(smallestFreq(S1, S2));
</script>
|
Complexity Analysis:
- Time Complexity: O(n), where n is the length of the string.
- A single traversal of the string is needed.
- Space Complexity: O(n).
- There can be at most n words in a string. The map requires O(n) space to store the strings.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Sep, 2022
Like Article
Save Article