Given a string S of length N, and Q queries of the following type:
Type 1: 1 i X
Update the i-th character of the string with the given character, X.
Type 2: L R
Count number of distinct characters in the given range [L, R].
- String S contains only lowercase alphabets.
Input: S = “abcdbbd” Q = 6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7
For the Queries:
1. L = 3, R = 6
The different characters are:c, b, d.
ans = 3.
2. String after query updated as S=”abcdzbd”.
3. L = 1, R = 1
Only one different character.
and so on process all queries.
Input: S = “aaaaa”, Q = 2
1 2 b
2 1 4
Query type 1: Replace i-th character of the string with the given character.
Query type 2: Traverse the string from L to R and count number of distinct characters.
Time complexity: O(N2)
Efficient approach: This approach is based on the Frequency-counting algorithm.
The idea is to use a HashMap to map distinct characters of the string with an Ordered_set which stores indices of its all occurrence. Ordered_set is used because it is based on Red-Black tree, so insertion and deletion of character will taken O ( log N ).
- Insert all characters of the string with its index into Hash-map
- For query of type 1, erase the occurrence of character at index i and insert occurrence of character X at index i in Hash-map
- For query of type 2, traverse all 26 characters and check if its occurrence is within the range [L, R], if yes then increment the count. After traversing print the value of count.
Below is the implementation of the above approach:
3 1 5
Time complexity: O(Q * logN) where Q is number of queries and N is the size of the string.
- Count of distinct characters in a substring by given range for Q queries
- Range Queries to count the number of even parity values with updates
- Array range queries to count the number of Fibonacci numbers with updates
- Array Range Queries to count Powerful numbers with updates
- Queries for count of array elements with values in given range with updates
- Range Queries for number of Armstrong numbers in an array with updates
- Array Range Queries to find the Maximum Armstrong number with updates
- Array range queries to find the maximum Fibonacci number with updates
- Array range queries to find the number of perfect square elements with updates
- Queries to count characters having odd frequency in a range [L, R]
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Range sum queries without updates
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- C++ Program for Range sum queries without updates
- Queries to find maximum product pair in range with updates
- Binary Indexed Tree : Range Updates and Point Queries
- Check whether count of distinct characters in a string is Prime or not
- Dynamic Segment Trees : Online Queries for Range Sum with Point Updates
- Count of Distinct strings possible by inserting K characters in the original string
- Count ways to partition a string such that both parts have equal distinct characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.