Given a string S of length N, and M queries of the following type:
Type 1: 1 L x,
Indicates update Lth index of string S by character ‘x’.
Type 2: 2 L R str
Find the number of subsets in range L to R
which is equal to the string str modulo 1000000007.
|S| <= 100000,
M <= 100000,
1 <= L, R <= |S|,
|str| <= 26,
All characters in str are unique,
S & str consisting of lowercase latin letters.
Input : N = 16, M = 3, S = “geeekkksgskeegks”
type = 2: L = 1, R = 7, str = “gek”
type = 1: index = 2, character = ‘x’
type = 2: L = 1, R = 7, str = “gek”
Output : 9 6 4
query2 : No of subsets in string S i range [1…7] that is equal to gek is 9.
query1 : string S is changed to gxeekkksgskeegks after second query.
query2 : No of subsets in string S i range [1…7] that is equal to gek is 6.
Naive Approach :
Query type 1: We will calculate the frequency of each character of the query string in the range [L…R] and then multiply all the calculated frequencies to get the desired result.
Query type 2: We will replace the i’th character of the string with the given character.
Time complexity : 0(m*n)
Efficient Approach :
- Using Segment Tree we can perform both operations in log(n) time. Every node of segment tree will contain frequency of characters in range [L..R].
- The function build takes n*log(n) time to create a segment tree with every node containing the frequency of characters of some segment of the string.
- The function get returns a vector containing frequency of all characters. Multiplication of all frequency of the given query string modulo 1e9+7 gives the desired result.
- The function update decreases the frequency of character placed earlier and increases the frequency of new character present in the nodes of the segment tree by one.
- The function add_two_result adds two vector and returns their result.
Below is the C++ implementation of the above approach:
Time complexity : 0(m*log(n)+n*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.
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Range Queries to Find number of sub-arrays with a given xor
- Binary Indexed Tree : Range Update and Range Queries
- Queries to find total number of duplicate character in range L to R in the string S
- Queries to update a given index and find gcd in range
- Find the element having maximum set bits in the given range for Q queries
- Array range queries to find the maximum Fibonacci number with updates
- Array Range Queries to find the Maximum Armstrong number with updates
- Array range queries to find the number of perfect square elements with updates
- Queries to check whether a given digit is present in the given Range
- Queries to count frequencies of a given character in a given range of indices
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Queries to print the character that occurs the maximum number of times in a given range
- Queries for Nth smallest character for a given range in a string
- Perform range sum queries on string as per given condition
- Queries to flip characters of a binary string in given range
- String Range Queries to count number of distinct characters with updates
- Queries to count array elements greater than or equal to a given number with updates
- Queries to find maximum product pair in range with updates
- Range Queries to find the Element having Maximum Digit Sum
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.