Most Frequent Word Problem Statement
You are given two strings 'A' and 'B' composed of words separated by spaces. Your task is to determine the most frequent and lexicographically smallest word in string 'A' that is not present in string 'B'. If no such word exists in 'A', return -1.
Example:
Input:
'A' = "coding ninjas coding ninjas", 'B' = "data structures and algorithms"
Output:
coding
Explanation:
The words 'coding' and 'ninjas' are both absent from string 'B' and occur twice in 'A'. However, 'coding' is lexicographically smaller than 'ninjas', so the result is "coding".
Constraints:
- 1 <= T <= 10
- 1 <= |A|, |B| <= 105 (Where |A|, |B| denote the length of strings 'A' and 'B' respectively)
- Time limit: 1 second
Note:
You don’t need to print anything. Implement the function and return the results as specified.
Input:
The first line contains an integer 'T' denoting the number of test cases. Each test case includes:
- First line: A string 'A' consisting of lowercase English letters only.
- Second line: A string 'B' consisting of lowercase English letters only.
Output:
For each test case, return the most frequent and lexicographically smallest word in string 'A' that is not present in string 'B'. If no such word exists, return -1.
Find the most frequent and lexicographically smallest word in string 'A' that is not present in string 'B'.
Split strings 'A' and 'B' into words
Count frequency of each word in 'A'
Check if word is not i...read more
Top Nagarro Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month