给定字符串S ,任务是查找同一字符出现的任何一对之间的最长子字符串的长度。
例子:
Input: S = “accabbacc”
Output: 6
Explanation: The longest substring satisfying the required conditions is “cabbac”, which lies between S[1](= ‘c’) and s[8](= ‘c’).
Input: S = “aab”
Output: 0
方法:请按照以下步骤解决问题:
- 初始化两个变量res和diff以分别存储最长的子字符串的长度和成对的相同字符之间的当前子字符串的长度。
- 从左到右遍历字符串的字符。
- 从右到左遍历右边的剩余字符串,直到当前字符。
- 如果获得两个相等的字符,即S [i] = S [j],则将它们之间的子字符串的长度存储在diff中。
- 将res的值更新为max(res,diff)。以便res存储到目前为止获得的所需类型的最长子字符串。
- 完整遍历字符串,将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)