📌  相关文章
📜  任意两次出现之间的最长子字符串-相似字符

📅  最后修改于: 2021-04-27 06:21:38             🧑  作者: Mango

给定字符串S ,任务是查找同一字符出现的任何一对之间的最长子字符串的长度。

例子:

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

  1. 初始化两个变量resdiff以分别存储最长的子字符串的长度和成对的相同字符之间的当前子字符串的长度。
  2. 从左到右遍历字符串的字符。
  3. 从右到左遍历右边的剩余字符串,直到当前字符。
  4. 如果获得两个相等的字符,即S [i] = S [j],则将它们之间的子字符串的长度存储在diff中
  5. res的值更新为max(res,diff)。以便res存储到目前为止获得的所需类型的最长子字符串。
  6. 完整遍历字符串,将res打印为必需的答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the longest substring
// between pair of repetitions of the same character
int longestbetweenequalcharacters(string S)
{
 
    // Length of the string
    int n = S.length();
 
    // Stores the maximum length and
    // length of current substring
    // satisfying given conditions
    int res = -1, diff = -1;
 
    // Traverse the string
    for (int i = 0; i < n - 1; i++) {
 
        // Search for repetition of S[i]
        for (int j = n - 1; j > i; j--) {
 
            // If repetition occurs
            if (S[i] == S[j]) {
 
                // Store the length of
                // the substring
                diff = j - i - 1;
 
                // Update maximum length of
                // substring obtained
                res = max(diff, res);
            }
        }
    }
 
    // Return obtained maximum length
    return res;
}
 
// Driver Code
int main()
{
    string s = "accabbacc";
    cout << longestbetweenequalcharacters(s);
}


Java
// Java program to implement
// the above approach
class GFG{
      
// Function to find the longest substring
// between pair of repetitions of the
// same character
static int longestbetweenequalcharacters(String S)
{
     
    // Length of the string
    int n = S.length();
  
    // Stores the maximum length and
    // length of current substring
    // satisfying given conditions
    int res = -1, diff = -1;
  
    // Traverse the string
    for(int i = 0; i < n - 1; i++)
    {
         
        // Search for repetition of S[i]
        for(int j = n - 1; j > i; j--)
        {
             
            // If repetition occurs
            if (S.charAt(i) == S.charAt(j))
            {
                 
                // Store the length of
                // the substring
                diff = j - i - 1;
  
                // Update maximum length of
                // substring obtained
                res = Math.max(diff, res);
            }
        }
    }
  
    // Return obtained maximum length
    return res;
}
  
// Driver code
public static void main(String[] args)
{
     
    String s = "accabbacc";
     
    System.out.println(
        longestbetweenequalcharacters(s));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
 
# Function to find the longest
# substring between pair of
# repetitions of the same character
def longestbetweenequalcharacters(S):
     
    # Length of the string
    n = len(S)
 
    # Stores the maximum length and
    # length of current substring
    # satisfying given conditions
    res = -1
    diff = -1
 
    # Traverse the string
    for i in range(n - 1):
         
        # Search for repetition of S[i]
        for j in range(n - 1, i, -1):
             
            # If repetition occurs
            if (S[i] == S[j]):
 
                # Store the length of
                # the substring
                diff = j - i - 1
 
                # Update maximum length of
                # substring obtained
                res = max(diff, res)
 
    # Return obtained maximum length
    return res
 
# Driver Code
if __name__ == '__main__':
     
    s = "accabbacc"
     
    print(longestbetweenequalcharacters(s))
 
# This code is contributed by doreamon_


C#
// C# program to implement
// the above approach 
using System;
 
class GFG{
       
// Function to find the longest substring
// between pair of repetitions of the
// same character
static int longestbetweenequalcharacters(String S)
{
     
    // Length of the string
    int n = S.Length;
   
    // Stores the maximum length and
    // length of current substring
    // satisfying given conditions
    int res = -1, diff = -1;
   
    // Traverse the string
    for(int i = 0; i < n - 1; i++)
    {
         
        // Search for repetition of S[i]
        for(int j = n - 1; j > i; j--)
        {
             
            // If repetition occurs
            if (S[i] == S[j])
            {
                 
                // Store the length of
                // the substring
                diff = j - i - 1;
   
                // Update maximum length of
                // substring obtained
                res = Math.Max(diff, res);
            }
        }
    }
   
    // Return obtained maximum length
    return res;
}
   
// Driver code
public static void Main()
{
    string s = "accabbacc";
      
    Console.WriteLine(
        longestbetweenequalcharacters(s));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
6

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