Given a numeric string S, the task is to find the maximum length of a subsequence having its left rotation equal to its right rotation.
Input: S = “100210601”
The subsequence “0000” satisfies the necessary condition.
The subsequence “1010” generates the string “0101” on left rotation and string “0101” on right rotation. Since both the rotations are same. Therefore, “1010” satisfies the condition as well.
Therefore, the maximum length of such subsequence is 4.
Input: S = “252525”
The subsequence “252525” generates the string “525252” on left rotation and string “525252” on right rotation. Since both the rotations are same. Therefore, the “252525” satisfies the required condition.
Naive Approach: The simplest approach to solve the problem is to generate all possible subsequences of the given string, and for each subsequence, check if its left rotation is equal to its right rotation.
Time Complexity: O(2N * N)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the main observation is that the subsequence should either consist of a single character or should be of even length consisting of two characters alternatively.
str = “2424”
Left rotation of the string = “4242”
Right rotation of the string = “4242”
As we can see, since the number is of even length having two characters appearing alternately, therefore, the left and right rotation of the given number is equal.
str = “24242”
Left rotation of the string = “42422”
Right rotation of the string = “22424”
As we can see, since the number is of odd length having two characters appearing alternately, therefore, the left and right rotation of the given number is not equal.
Follow the steps below to solve the problem:
- Generate all possible two-digit numbers.
- For each two-digit number generated, check for the alternating occurrence of both the digits in the string. Keep incrementing count to store length of alternating subequence for the particular combination.
- After entire traversal of the string, check if both the digits are equal or not. If found to be true, update count to the required answer. If both the digits are equal, then update count or count – 1 to the answer if count is even or odd respectively.
- Repeat the above steps for all the possible combinations and print the maximum count obtained.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Left Rotation and Right Rotation of a String
- Minimize characters to be changed to make the left and right rotation of a string same
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Find the length of the longest subsequence with first K alphabets having same frequency
- Print left rotation of array in O(n) time and O(1) space
- Left rotation of an array using vectors in C++
- Length of longest subsequence having sum of digits of each element as a Composite Number
- Reversal algorithm for right rotation of an array
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Subsequence pair from given Array having all unique and all same elements respectively
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest subsequence having equal numbers of 0 and 1
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Maximize count of corresponding same elements in given Arrays by Rotation
- Longest subsequence having greater corner values
- Find the longest subsequence of an array having LCM at most K
- Longest subsequence having difference atmost K
- Longest Increasing Subsequence having sum value atmost K
- Length of longest subsequence in an Array having all elements as Nude Numbers
- Length of longest subsequence having absolute difference of all pairs divisible by K
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.