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


In this problem, we need to find the length of the longest subsequence having the same left and right rotations.

We can solve the problem using the brute-force approach, finding all possible subsequences of the given string and checking each subsequence one by one to see if it has the same left and right rotation. We find the maximum length of such subsequence. The problem with this approach is that it is very time-consuming as its time complexity is O(2N).

So, we will learn to write optimized code in this approach.

Problem statement – We have given string str containing N numeric characters. We need to find the longest subsequence of the given string having the same left and right rotations.

Sample examples

Input– str = "9898798"

Output– 6

Explanation– We have the longest subsequence having the same left, and right rotation is 989898.

Input– str = ‘12345678’

Output– 2

Explanation– We can choose any subsequence of length 2 as it always has the same left and right rotation.

Input– ‘327787767777’

Output– 8

Explanation– The longest subsequence having the same left and right rotation is ‘77777777’.

Approach 1

In this approach, 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.

Here, we will try to find the subsequence of the same or alternating digits.

Algorithm

  • Initialize the ‘len’ variable with string length and ‘cnt’ with 0 to store the maximum length of the subsequence.

  • Use two nested loops to traverse from 0 to 9 and get a combination of each digit.

  • Define the ‘cur_len’ variable to store the length of the current subsequence. Also, define the ‘firstDigit’ variable to track the next digit is an pth or qth digit in the altering sequence of digits.

  • Use the third nested loop to traverse the numeric string.

  • If firstDigit == 0 && str.charAt(k) - '0' ==pI is true, change the value of firstDigit to 1 and increment cur by 1.

  • If firstDigit == 1 && str.charAt(k) - '0' == q is true, change the value of firstDigit to 0 and increment ‘cur_len’ by 1.

  • If p and q are not the same, and the value of ‘cur_len’ is odd, decrease the ‘cur_len’ by 1.

  • Update the value of ‘cnt’ if the value of ‘cur_len’ is greater than the ‘cnt’ value.

  • Return the ‘cnt’ value.

Example

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.

In this tutorial, we learned to find the maximum length of the subsequence having the same left and right rotations. Programmers can try to solve using the brute force approach by using the code to find all possible subsequences of the given string.

Updated on: 17-Aug-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements