Average CTC: 20lpa
Company: Product based
Roles: Software Engineer, Software Developer
You are given two strings A and B that are amde of lowercase English alphabets. Find the number of different pairs ((i,j),(k,l)) such that the substrings A[i.....j] and B[k.....l] are equal and the value of j-i+1 is minimum.
Example
Consider A = 'abc' and B = 'bcd'. You must determine the answer such that it meets the given conditions:
Since we need to minimize the length of matching substring hence the answer to the corresponding example is 2.
Function Description
Complete the solve function provided in the editor. This function takes the following 2 parameters
Input Format
Note: This is the input format that you must use to provide custom input
The first line consists of a string denoting A
The second line consists of a string denoting B
Output
Print the number of different pairs ((i,j),(k,l)) such that the substrings A[i...j] and B[k...l] are equal and the value of j-i+1 is minimum.
Constraints
1<= |A|,|B|<=10^5
Sample Input
abdc
bd
Sample Output
2
You are given a string S[1....n] of length n having only lowercase English letters.
You are also given q queries, each having two space-separated integers l and r.
For each query, you need to print the number of substrings of the string S[l...r] which starts and end with the same character.
Notes:
Example
Consider n=4, q=2, s=aabc. You must determine the cost of substring such that it meets the following conditions:
for 1st query, l=1, r=2. We have 3 substring that satisfy the above condition, s[1,1], s[1,2], s[2,2].
for 2nd query, l=3, r=4. We have 2 substring that satisfy the above condition, s[3,3], s[4,4].
Function description
Complete the Sub_string_sol function provided in the editor. This function takes the following 4 parameters and returns the array of size q representing the number of required substring for each query:
Input Format
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button)
First line of the input will have two space-separated integers denoting the values of n and q respectively.
Second line of the input will have string S
In the next q lines, each line will have two space-separated integers l and r respectively.
Output Format
For every query, print the integer answer in the separate lines.
Constraints
Sample Input
6 4
abcaab
1 1
2 5
3 6
1 6
Sample Output
1
5
5
10
Since it is required to minimise j-i+1
, we can always choose i=j
and hence we have to just determine the pairs of equal characters in the given strings. Thus we can find the count of each character in the given strings and the corresponding pairs will be count_in_string_1[char]*count_in_string_2[char]
. We can add the pairs for each character to calculate the total pairs.
solve(s,t):
count1 = count2 = [0]*26
for c in s: //counting the frequency of each character in s
count1[c]++
for c in t: //counting the frequency of each character in s
count2[c]++
ans = 0
for c from 'a' to 'z': //total number of pairs is the sum of pairs for each character
ans += count1[c]*count2[c] //number of pairs for a given character is just the multiplication of the frequency of the character in two strings
return ans
Let's first note that if there are x
occurences of some character c
in a string, the number of substrings that start and end with c
are equal to x*(x+1)/2
(As there are x
unit length substrings and C(x,2)
other substrings, where C(x,2) = x*(x-1)/2
). Thus we can determine the total number of required substrings if we know the frequency of each character.
To answer the queries efficiently, we can build a prefix sum array for each character to determine the frequency of the character in the specified range. We can then use the expression explained above to obtain the total number of required substrings. Thus each query is answered in O(w)
where w
is the alphabet size. The precomputation has a time complexity and space complexity of O(w*N)
.
solve(s,queries):
n = len(s)
pre[][] = int[n][26]
pre[0][s[0]-'a'] = 1 //calculating the prefix sum for each character
for i from 1 to n:
for c from 0 to 26:
pre[i][c] = pre[i-1][c]
pre[i][s[i]-'a']++
ans = []
for query in queries:
current = 0
for c from 0 to 26: //to answer a query, we add the number of substrings for each character separately
freq = pre[query.r] - pre[query.l-1]
current += (freq*(freq+1))/2 //As explained the number of substrings for a character with frequency freq is freq*(freq+1)/2
ans.append(current)
return ans