Stream Of Characters
You are given a list, ‘DICTIONARY[]’ containing a list of words and a stream of characters (queries). Your task is to choose a suitable data structure to implement ‘CharacterStreamChecker’ class described as follows:
1) ‘CharacterStreamChecker(dictionary)’: Constructor to initialize the data structure with given words present in ‘DICTIONARY[]’.
2) ‘solveQuery(character)’: Function to check whether the string formed by last ‘C’ (C >= 1) queried characters (in order from oldest to newest, including the character just queried) is present in the ‘DICTIONARY[]’. If yes, return true. Otherwise, return false.
Note :
The ‘DICTIONARY[]’ contains only distinct strings.
Input Format :
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains an integer ‘N’ representing the number of strings in the list, ‘DICTIONARY[]’.
The second line of each test case contains ‘N’ space-separated strings present in the list, ‘DICTIONARY[]’.
The third line of each test case contains an integer ‘Q’ representing the number of queries (or characters in the stream).
The last line of each test case contains ‘Q’ space-separated characters.
Output Format :
For each query in a test case, return true if the string formed by the last ‘C’ characters (C >= 1) is present in the ‘DICTIONARY[]’. Otherwise, return false.
The output of each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= N <= 200
1 <= |DICTIONARY[i]| <= 200
1 <= Q <= 400
‘DICTIONARY[i]’ and ‘QUERY’ contains only lowercase english letters.
Time limit: 1 second
CodingNinjas
author
2y
Brute Force
The idea is to use the brute force approach for this problem. At every queried character, we will form strings in a backward direction and will simultaneously check for the string in the ‘D...read more
CodingNinjas
author
2y
Trie Data Structure
It is not easier to develop a solution to this problem by the usual approach. But if we look at the problem in a way to match the reverse of ‘C’ stream of characters with reversed s...read more
Help your peers!
Add answer anonymously...
Top Amazon Research Analyst interview questions & answers
Popular interview questions of Research Analyst
Top HR questions asked in Amazon Research Analyst
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app