给定一个字符串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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live