Open In App

Python3 Program for Longest subsequence of a number having same left and right rotation

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Examples:

Input: S = “100210601” 
Output:
Explanation: 
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” 
Output:
Explanation: 
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.

Illustration: 
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 the length of alternating subsequence for the particular combination.
  • After the 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 the 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:

Python3




# Python3 program to implement
# the above approach
import sys
 
# Function to find the longest subsequence
# having equal left and right rotation
def findAltSubSeq(s):
     
    # Length of the string
    n = len(s)
    ans = -sys.maxsize - 1
     
    # Iterate for all possible combinations
    # of a two-digit numbers
    for i in range(10):
        for j in range(10):
            cur, f = 0, 0
             
            # Check for alternate occurrence
            # of current combination
            for k in range(n):
                if (f == 0 and ord(s[k]) -
                               ord('0') == i):
                    f = 1
                     
                    # Increment the current value
                    cur += 1
                 
                elif (f == 1 and ord(s[k]) -
                                 ord('0') == j):
                    f = 0
                     
                    # Increment the current value
                    cur += 1
             
            # If alternating sequence is
            # obtained of odd length
            if i != j and cur % 2 == 1:
                 
                # Reduce to even length
                cur -= 1
                 
            # Update answer to store
            # the maximum
            ans = max(cur, ans)
             
    # Return the answer
    return ans
 
# Driver code
s = "100210601"
 
print(findAltSubSeq(s))
 
# This code is contributed by Stuti Pathak


Output: 

4

 

Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time 
Auxiliary Space: O(1), as we are not using any extra space.
 

Please refer complete article on Longest subsequence of a number having same left and right rotation for more details!



Last Updated : 27 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads