Given a string S of size N consisting of lower case alphabets and an integer Q which represents the number of queries for S. Our task is to print the number of duplicate characters in the substring L to R for all the queries Q.
Note: 1 ≤N ≤ 106 and 1 ≤ Q≤ 106
S = “geeksforgeeks”, Q = 2
L = 1 R = 5
L = 4 R = 8
For the first query ‘e’ is the only duplicate character in S from range 1 to 5.
For the second query theres is no duplicate character in S.
S = “Geekyy”, Q = 1
L = 1 R = 6
For the first query ‘e’ and ‘y’ are duplicate characters in S from range 1 to 6.
The naive approach would be to maintain a frequency array of size 26, to store the count of each character. For each query, given a range [L, R] we will traverse substring S[L] to S[R] and keep counting the occurrence of each character. Now, if the frequency of any character is greater than 1 then we would add 1 to answer.
To solve the above problem in an efficient way we will store the position of each character as it appears in the string in a dynamic array. For each given query we will iterate over all the 26 lower case alphabets. If the current letter is in the substring S[L: R] then the next element of the first element which is greater than or equal L to in the corresponding vector should exist and be less than or equal to R.
Diagram below shows how we store characters in the dynamic array:
Below is the implementation of the above approach:
Time complexity: O( Q * 26 * log N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- String Range Queries to find the number of subsets equal to a given String
- Queries for Nth smallest character for a given range in a string
- Queries to print the character that occurs the maximum number of times in a given range
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Range Queries to Find number of sub-arrays with a given xor
- String Range Queries to count number of distinct characters with updates
- Find total number of distinct years from a string
- Queries for rotation and Kth character of the given string in constant time
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Find a string such that every character is lexicographically greater than its immediate next character
- Count number of indices such that s[i] = s[i+1] : Range queries
- Queries for number of array elements in a range with Kth Bit Set
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Find the character in first string that is present at minimum index in second string
- Queries to find frequencies of a string within specified substrings
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Queries to find the count of vowels in the substrings of the given string
- Find k-th character of decrypted string | Set - 2
- Find the last non repeating character in string