Given a string X. Form a string S by repeating string X multiple times i.e appending string X multiple times with itself. There are Q queries of form i and j. The task is to print “Yes” if the element at index i is same as the element at index j in S else print “No” for each query.
Input : X = "geeksforgeeks", Q = 3. Query 1: 0 8 Query 2: 8 13 Query 3: 6 15 Output : Yes Yes No String S will be "geeksforgeeksgeeksforgeeks....". For Query 1, index 0 and index 8 have same element i.e 'g'. For Query 2, index 8 and index 13 have same element i.e 'g'. For Query 3, index 6 = 'o' and index 15 = 'e' which are not same.
Let length of string X be n. Observe that element at indexes 0, n, 2n, 3n,…. are same. Similarly for index i, position i, n+i, 2n+i, 3n+i,….. contain same element.
So, for each query, find (i%n) and (j%n) and if both are same for string X.
Below is the implementation of above idea :
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 email@example.com. 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.
- Return maximum occurring character in an input string
- Print all the duplicates in the input string
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Divide a string in N equal parts
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Print list items containing all characters of a given word
- Reverse words in a given string
- Find the smallest window in a string containing all characters of another string
- Length of the longest substring without repeating characters
- Print all permutations with repetition of characters
- Interleaving of two given strings with no common characters