最长的子字符串,其任何非空子字符串都不是给定字符串的前缀或后缀
给定一个长度为N的字符串S ,任务是找到字符串S的最长子串X的长度,使得:
- X的任何非空子串都不是 S 的前缀。
- X的任何非空子串都不是 S 的后缀。
- 如果不可能有这样的字符串,则打印 -1。
例子:
Input: S = “abcdefb”
Output: 4
Explanation: cdef is the substring satisfying the conditions.
Input: S = “cccc”
Output: -1
Explanation: No substring can satisfy the conditions.
方法:要解决问题,请遵循以下观察:
观察:
Since a prefix starts from a starting of string S and a suffix starts from ending of a string S. Therefore maximum length of substring can be length of string S – 2 and it cannot have elements which are at the starting or ending of the string.
So select the substring not having any character which is the first or last character of the string. this will the largest substring satisfying the conditions.
请按照提到的步骤解决问题:
- 将字符串从i = 1 遍历到字符串的末尾。
- 检查是否找到不等于字符串的第一个和最后一个字符的字符。
- 如果找到,则将其视为结果子字符串的一部分。
- 否则,它不会包含在子字符串中。
- 结果将是满足条件的子字符串的最大长度。
下面是上述方法的实现:
C++
// C++ code to implement the above approach
#include
using namespace std;
// Function to find the largest substring
int findLargest(string s)
{
int maxi = -1;
int sum = 0;
// Loop to find the largest substring
// satisfying the given conditions
for (int i = 1; i < s.size() - 1; i++) {
if (s[i] != s[0] && s[i] != s[s.length() - 1]) {
sum++;
}
else {
maxi = max(sum, maxi);
sum = 0;
}
}
maxi = max(sum, maxi);
if (maxi == 0) {
maxi = -1;
}
// Return the maximum length
return maxi;
}
// Driver code
int main()
{
string s = "abcdefb";
// Function call
cout << (findLargest(s));
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java code to implement the above approach
import java.io.*;
class GFG {
// Function to find the largest substring
public static int findLargest(String s)
{
int max = -1;
int sum = 0;
// Loop to find the largest substring
// satisfying the given conditions
for (int i = 1; i < s.length() - 1; i++) {
if (s.charAt(i) != s.charAt(0)
&& s.charAt(i)
!= s.charAt(s.length() - 1)) {
sum++;
}
else {
max = Math.max(sum, max);
sum = 0;
}
}
max = Math.max(sum, max);
if (max == 0) {
max = -1;
}
// Return the maximum length
return max;
}
// Driver code
public static void main(String[] args)
{
String s = "abcdefb";
// Function call
System.out.println(findLargest(s));
}
}
Python
# Python code to implement the above approach
# Function to find the largest substring
def findLargest(s):
maxi = -1
sum = 0
# Loop to find the largest substring
# satisfying the given conditions
for i in range(1, len(s) - 1):
if (s[i] != s[0] and s[i] != s[len(s) - 1]):
sum += 1
else:
maxi = max(sum, maxi)
sum = 0
maxi = max(sum, maxi)
if (maxi == 0):
maxi = -1
# Return the maximum length
return maxi
# Driver code
if __name__ == "__main__":
s = "abcdefb"
# Function call
print(findLargest(s))
# This code is contributed by hrithikgarg03188.
C#
// C# code to implement the above approach
using System;
public class GFG{
// Function to find the largest substring
public static int findLargest(string s)
{
int max = -1;
int sum = 0;
// Loop to find the largest substring
// satisfying the given conditions
for (int i = 1; i < s.Length - 1; i++) {
if (s[i] != s[0] && s[i] != s[s.Length - 1]) {
sum++;
}
else {
max = Math.Max(sum, max);
sum = 0;
}
}
max = Math.Max(sum, max);
if (max == 0) {
max = -1;
}
// Return the maximum length
return max;
}
// Driver code
static public void Main (){
string s = "abcdefb";
// Function call
Console.Write(findLargest(s));
}
}
// This code is contributed by hrithikgarg03188.
4
时间复杂度: O(N)
辅助空间: O(1)