Ninja and the Game of Words

In this game, Ninja is provided with a string STR that might contain spaces, and a list or array WORDS consisting of N word strings. Ninja's task is to determine how many times each word in WORDS appears in STR.

Input:

The first line contains an integer T, representing the number of test cases.
The first line of each test case contains an integer N.
The second line contains the string STR.
The following N lines each contain a word from WORDS.

Output:

For each test case, output the frequency of each word in WORDS as it appears in STR. Print each result on a new line, maintaining the order given in WORDS.

Example:

Input:
T = 1
N = 3
STR = "i am a Ninja"
WORDS = ["Ninja","a","am"]

Output:
[1,3,1]

Constraints:

  • 1 <= T <= 100
  • 1 <= |STR| <= 4000
  • 1 <= N <= 4000
  • 1 <= |WORDS[i]| <= 4000

Note:

The output must maintain the order of words as provided in WORDS. You don't have to print anything, just implement the function to return the answer.

AnswerBot
4mo

The task is to determine the frequency of each word in a list of words that appears in a given string.

  • Iterate through each word in the list of words and count its frequency in the given string.

  • Maintai...read more

Help your peers!
Select
Add answer anonymously...

Goldman Sachs Analyst interview questions & answers

An Analyst was asked 10mo agoQ. What is a strength of yours?
An Analyst was asked Q. How do you merge multiple sorted arrays into one sorted array?
An Analyst was asked Q. Explain what a Binary Search Tree is.

Popular interview questions of Analyst

An Analyst was asked Q1. How do you merge multiple sorted arrays into one sorted array?
An Analyst was asked Q2. Explain what a Binary Search Tree is.
An Analyst was asked Q3. Given a matrix where each row and each column is sorted in ascending order, how ...read more
Goldman Sachs Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits