Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python3 Program for Longest subsequence of a number having same left and right rotation
In this problem, we will find the length of the longest subsequence of a given numeric string such that it has the same left and right rotation. A string has the same left and right rotation when rotating left by one position produces the same result as rotating right by one position.
We can solve this problem using two approaches: generating all subsequences and checking rotations, or using an optimized observation-based method that recognizes patterns in valid subsequences.
Problem Statement
Given a numeric string, find the size of the longest subsequence that has the same left and right rotations.
Sample Examples
Input:
alpha = "700980503"
Output:
4
Explanation: The longest subsequence having the same left and right rotation is '0000'.
Input:
alpha = "19199019"
Output:
6
Explanation: The resultant subsequence is '191919'.
Approach 1: Generate All Subsequences
This approach generates all possible subsequences and checks if each has the same left and right rotation using string slicing.
Algorithm
Step 1: Initialize 'maxLen' to store the maximum length found.
Step 2: Generate all subsequences of the input string.
Step 3: For each subsequence, check if left rotation equals right rotation.
Step 4: Track the maximum length of valid subsequences.
Example
def getSubs(alpha):
allSubs = ['']
for ch in alpha:
temp = allSubs[:]
for subseq in temp:
allSubs.append(subseq + ch)
return allSubs
def maxSubSeqLen(alpha):
maxLen = 0
allSubs = getSubs(alpha)
for subseq in allSubs:
if len(subseq) <= 1:
maxLen = max(maxLen, len(subseq))
continue
# Left rotation: move first character to end
left_rotation = subseq[1:] + subseq[:1]
# Right rotation: move last character to front
right_rotation = subseq[-1:] + subseq[:-1]
if left_rotation == right_rotation:
maxLen = max(maxLen, len(subseq))
return maxLen
alpha = "19199019"
print("The maximum length of subsequence having the same left and right rotations is:")
print(maxSubSeqLen(alpha))
Output:
The maximum length of subsequence having the same left and right rotations is: 6
Time Complexity: O(2N) for generating all subsequences.
Space Complexity: O(2N) for storing all subsequences.
Approach 2: Optimized Pattern-Based Solution
This approach uses the observation that a string has the same left and right rotation only if it contains all identical characters or alternating pairs of characters with even total length.
Algorithm
Step 1: For each pair of digits (0-9), find the longest alternating subsequence.
Step 2: Track whether we're looking for the first or second character of the pair.
Step 3: For different pairs with odd length, reduce by 1 to make it even.
Step 4: Return the maximum length found.
Example
def maxSubSeqLen(alpha):
maxLen = 0
# Try all pairs of digits from 0 to 9
for p in range(10):
for q in range(10):
currMax = 0
isSecond = 0 # 0: looking for p, 1: looking for q
# Find alternating pattern of p and q
for char in alpha:
digit = int(char)
if isSecond == 0 and digit == p:
isSecond = 1
currMax += 1
elif isSecond == 1 and digit == q:
isSecond = 0
currMax += 1
# For different digits with odd length, make it even
if p != q and currMax % 2 == 1:
currMax -= 1
maxLen = max(maxLen, currMax)
return maxLen
# Test with different examples
alpha = "700980503"
print(f"Input: {alpha}")
print(f"Maximum length: {maxSubSeqLen(alpha)}")
alpha = "19199019"
print(f"Input: {alpha}")
print(f"Maximum length: {maxSubSeqLen(alpha)}")
Output:
Input: 700980503 Maximum length: 4 Input: 19199019 Maximum length: 6
Time Complexity: O(100 × N) ? O(N)
Space Complexity: O(1)
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| All Subsequences | O(2N) | O(2N) | Small strings, educational purpose |
| Pattern-Based | O(N) | O(1) | Large strings, optimal solution |
Conclusion
The pattern-based approach is significantly more efficient with O(N) time complexity compared to the exponential brute force method. It leverages the mathematical property that only strings with identical characters or alternating patterns can have equal left and right rotations.
---