Given a string S, and two type of queries.
Type 1: 1 L x, Indicates update Lth index of string S by x character. Type 2: 2 L R, Find if characters between position L and R of string S can form a palindrome string. If palindrome can be formed print "Yes", else print "No". 1 <= L, R <= |S|
Input : S = "geeksforgeeks" Query 1: 1 4 g Query 2: 2 1 4 Query 3: 2 2 3 Query 4: 1 10 t Query 5: 2 10 11 Output : Yes Yes No Query 1: update index 3 (position 4) of string S by character 'g'. So new string S = "geegsforgeeks". Query 2: find if rearrangement between index 0 and 3 can form a palindrome. "geegs" is palindrome, print "Yes". Query 3: find if rearrangement between index 1 and 2 can form a palindrome. "ee" is palindrome, print "Yes". Query 4: update index 9 (position 10) of string S by character 't'. So new string S = "geegsforgteks". Query 3: find if rearrangement between index 9 and 10 can form a palindrome. "te" is not palindrome, print "No".
Substring S[L…R] form a palindrome only if frequencies of all the character in S[L…R] are even, with one except allowed.
For query of type 1, simply update string S[L] by character x. For each query of type 2, calculate the frequency of character and check if frequencies of all characters is even (with) one exception allowed.
Following are two different methods to find frequency of each character in S[L…R]:
Method 1: Use a frequency array to find the frequency of each element in S[L…R].
Below is the implementation of this approach:
Yes Yes No
Method 2 : Use Binary Indexed Tree
The efficient approach can be maintain 26 Binary Index Tree for each alphabet.
Define a function getFrequency(i,u) which returns the frequency of ‘u’ in the ith prefix. Frequency of character ‘u’ in range L…R can be find by getFrequency(R, u) – getFrequency(L-1, u).
Whenever update(Query 1) comes to change S[i] from character ‘u’ to ‘v’. BIT[u] is updated with -1 at index i and BIT[v] is updated with +1 at index i.
Below is C++ implementation of this approach:
Yes Yes No
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Palindrome Substring Queries
- Word formation using concatenation of two dictionary words
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Array range queries over range queries
- Range LCM Queries
- Range sum queries without updates
- Queries on subsequence of string
- Min-Max Range Queries in Array
- Range product queries in an array
- Queries on probability of even or odd number in given ranges
- Queries for characters in a repeated string
- Queries on XOR of greatest odd divisor of the range