Substring Queries
Practice
3.8 (29 votes)
Ad Hoc
Algorithms
Approved
Medium
Open
Two pointer
Problem
69% Success 2945 Attempts 30 Points 3s Time Limit 256MB Memory 1024 KB Max Code

Given a string S, answer Q queries.
Each query contains a string qstr. Please output the number of substrings of S that contain some anagram of qstr as a subsequence.

Input Format:

The first line of input file contains an integer T that denotes the number of test cases to follow.
Each of the test cases contain the string S in first line. The next line contains an integer Q. Each of the next Q lines contain a query string qstr.

Output Format:

The output file should contain answers for each query in a new line.

Constraints:

1 ≤ T ≤ 3
1 ≤ |S| ≤ 105
1 ≤ Q ≤ 200
None of the characters in qstr shall be repeated.
All the strings will only contain characters from A-Z, a-z and 0-9.

Notes:

  • The substrings are considered different only if there start or end positions differ.
  • Subsequence is a sequence of characters that you obtain upon deletion of 1 or more character from the string.

Sample Explanation:

Case #1:

a occurs in following substrings:

a 0..0
ab 0..1
aba 0..2
ba 1..2
a 2..2

(the start and end points here, are inclusive)

Case #2:

dba occurs as its anagram abd in abcd at 0..3
dba occurs as its anagram bda in bcda at 1..4
dba occurs as its anagram dba in abcda at 0..4

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
17 votes
Tags:
ApprovedBasic ProgrammingBit manipulationMathMedium
Points:30
6 votes
Tags:
Bit ManipulationBasics of Bit ManipulationBasic ProgrammingObservationMathImplementation
Points:30
13 votes
Tags:
Basic ProgrammingBit manipulationMedium