
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Longest Subsequence of a Number Having Same Left and Right Rotation in Java
In this tutorial, we will learn how to find the maximum length of the subsequence having the same left and right rotations in Java. We find the maximum length of such subsequence.
Finding the longest subsequence with identical or alternating digits
We will solve the problem based on a particular observation. The strings can only have the same left and right rotations if all digits of strings are equal or it contains the two digits alternatively, and the string length is even.
- Step 1: Initialize Variables: Initialize the len variable with the string length and cnt with 0 to store the maximum length of the subsequence.
int len = str.length(), cnt = 0;
- Step 2: Traverse Each Digit Combination: Use two nested loops to traverse digits from 0 to 9, getting all possible combinations of digits p and q.
for (int p = 0; p < 10; p++) {
for (int q = 0; q < 10; q++) {
- Step 3: Initialize Subsequence Variables: Define the cur_len variable to store the length of the current subsequence. Also, define the firstDigit variable to track whether the next digit is p or q in the alternating sequence.
int cur_len = 0, firstDigit = 0;
- Step 4: Traverse the Numeric String: Use a third nested loop to traverse the numeric string and check for alternating subsequences of digits p and q.
for (int r = 0; r < len; r++) {
-
Step 5: Check and Update Subsequence:
If firstDigit == 0 and the current character equals p, change firstDigit to 1 and increment cur_len by 1.
If firstDigit == 1 and the current character equals q, change firstDigit to 0 and increment cur_len by 1.
if (firstDigit == 0 && str.charAt(r) - '0' == p) {
firstDigit = 1;
cur_len++;
} else if (firstDigit == 1 && str.charAt(r) - '0' == q) {
firstDigit = 0;
cur_len++;
}
- Step 6: Adjust Odd-Length Subsequence: If p and q are not the same, and the value of cur_len is odd, reduce cur_len by 1 to ensure an even subsequence.
if (p != q && cur_len % 2 == 1)
cur_len--;
- Step 7: Update Maximum Length: Update the value of cnt if cur_len is greater than the current value of cnt.
cnt = Math.max(cur_len, cnt);
Java program for longest subsequence with rotation
The following isan example of a Java program for the longest subsequence of a number having the same left and right rotation
import java.util.*; public class Main { static int findLongestSub(String str) { int len = str.length(), cnt = 0; // Traverse each combination of the string. for (int p = 0; p < 10; p++) { for (int q = 0; q < 10; q++) { int cur_len = 0, firstDigit = 0; // Find the alternating sequence for (int r = 0; r < len; r++) { if (firstDigit == 0 && str.charAt(r) - '0' == p) { firstDigit = 1; // add 1 cur_len++; } else if (firstDigit == 1 && str.charAt(r) - '0' == q) { firstDigit = 0; // add 1 cur_len++; } } // If the current value is odd, decrease it by 1 if (p != q && cur_len % 2 == 1) cur_len--; // Update the cnt value cnt = Math.max(cur_len, cnt); } } // Return the result return cnt; } public static void main(String[] args) { String str = "9898798"; System.out.print("The length of the longest subsequence having the same left and right rotations is " + findLongestSub(str)); } }
Output
The length of the longest subsequence having the same left and right rotations is 6
Time complexity - O(N) as we traverse the numeric string of length K.
Space complexity - O(1) as we don't use any dynamic space.
Advertisements