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.
AnswerBot
1mo

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

Help your peers!
Add answer anonymously...
Nagarro Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter