Java Program for Longest subsequence of a number having same left and right rotation
Last Updated :
27 May, 2022
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: 4
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: 6
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:
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the longest subsequence
// having equal left and right rotation
static int findAltSubSeq(String s)
{
// Length of the String
int n = s.length(), ans = Integer.MIN_VALUE;
// Iterate for all possible combinations
// of a two-digit numbers
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
int cur = 0, f = 0;
// Check for alternate occurrence
// of current combination
for (int k = 0; k < n; k++)
{
if (f == 0 && s.charAt(k) - '0' == i)
{
f = 1;
// Increment the current value
cur++;
}
else if (f == 1 &&
s.charAt(k) - '0' == j)
{
f = 0;
// Increment the current value
cur++;
}
}
// If alternating sequence is
// obtained of odd length
if (i != j && cur % 2 == 1)
// Reduce to even length
cur--;
// Update answer to store
// the maximum
ans = Math.max(cur, ans);
}
}
// Return the answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
String s = "100210601";
System.out.print(findAltSubSeq(s));
}
}
// This code is contributed by PrinciRaj1992
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!
Similar Reads
Java Program to Minimize characters to be changed to make the left and right rotation of a string same Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = âabcdâOutput: 2Explanation:String after the left shift: âbcdaâString after the right shift: âdabc
3 min read
Java Program for Longest Increasing Subsequence The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60,
5 min read
Java Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31
2 min read
Java Program for Longest Common Subsequence LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. For example, "abc", "abg", "bdf", "aeg", '"acefg", .. etc are subsequences of "abcdefg". So
4 min read
Java Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types: If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array. Examples:Â Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2,
5 min read