📌  相关文章
📜  具有相同左右旋转的数字的最长子序列

📅  最后修改于: 2021-09-03 04:08:35             🧑  作者: Mango

给定一个数字字符串S ,任务是找到左旋转等于右旋转的子序列的最大长度。

例子:

朴素方法:解决问题的最简单方法是生成给定字符串 的所有可能子序列,并对每个子序列检查其左旋转是否等于右旋转。
时间复杂度: O(2 N * N)
辅助空间: O(N)

有效的方法:为了优化上述方法,主要观察是子序列应该由单个字符组成,或者应该是由两个字符交替组成的偶数长度。

请按照以下步骤解决问题:

  • 生成所有可能的两位数。
  • 对于生成的每个两位数,检查字符串中两个数字是否交替出现。保持递增计数以存储特定组合的交替子序列的长度。
  • 在整个遍历字符串,检查两个数字是否相等。如果发现是真的,请将计数更新为所需的答案。如果两个数字相等,那么如果计数分别是偶数或奇数,则更新countcount – 1到答案。
  • 对所有可能的组合重复上述步骤并打印获得的最大计数

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the longest subsequence
// having equal left and right rotation
int findAltSubSeq(string s)
{
    // Length of the string
    int n = s.size(), ans = INT_MIN;
 
    // 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 and s[k] - '0' == i) {
                    f = 1;
 
                    // Increment the current value
                    cur++;
                }
                else if (f == 1 and s[k] - '0' == j) {
                    f = 0;
 
                    // Increment the current value
                    cur++;
                }
            }
 
            // If alternating sequence is
            // obtained of odd length
            if (i != j and cur % 2 == 1)
 
                // Reduce to even length
                cur--;
 
            // Update answer to store
            // the maximum
            ans = max(cur, ans);
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    string s = "100210601";
    cout << findAltSubSeq(s);
 
    return 0;
}


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


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


C#
// C# Program to implement
// the above approach
using System;
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 = int.MinValue;
 
    // 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[k] - '0' == i)
                {
                    f = 1;
 
                    // Increment the current value
                    cur++;
                }
                else if (f == 1 &&
                         s[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";
    Console.Write(findAltSubSeq(s));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(1)