鉴于小写字母的字符串str,任务是找到按字母顺序排列,即字符串的字符“DF ABC K”将返回3.请注意,这里的字母顺序被认为是圆形的,即A,B最长的子字符串的长度,c,d,e,…,x,y,z,a,b,c,… 。
例子:
Input: str = “abcabcdefabc”
Output: 6
All valid sub-strings are “abc”, “abcdef” and “abc”
And, the length of the longest of these is 6
Input: str = “zabcd”
Output: 5
方法:
- 初始化i = 0和len = 0,然后从i开始,找到最长的有效子字符串的结束索引,并将其存储在end中。
- 现在,更新len = max(end-i + 1,len)和i = end +1 (以从索引end + 1开始获得下一个有效的子字符串),然后重复步骤1直到i
为止。 - 最后打印len的值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the ending index for the
// largest valid sub-string starting from index i
int getEndingIndex(string str, int n, int i)
{
i++;
while (i < n)
{
char curr = str[i];
char prev = str[i-1];
// If the current character appears after
// the previous character according to
// the given circular alphabetical order
if ((curr == 'a' && prev == 'z') ||
(curr - prev == 1))
i++;
else
break;
}
return i - 1;
}
// Function to return the length of the longest
// sub-string of consecutive characters from str
int largestSubStr(string str, int n)
{
int len = 0;
int i = 0;
while (i < n)
{
// Valid sub-string exists from index
// i to end
int end = getEndingIndex(str, n, i);
// Update the length
len = max(end - i + 1, len);
i = end + 1;
}
return len;
}
// Driver code
int main()
{
string str = "abcabcdefabc";
int n = str.length();
cout << (largestSubStr(str, n));
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation of the approach
class GFG {
// Function to return the ending index for the
// largest valid sub-string starting from index i
static int getEndingIndex(String str, int n, int i)
{
i++;
while (i < n) {
char curr = str.charAt(i);
char prev = str.charAt(i - 1);
// If the current character appears after
// the previous character according to
// the given circular alphabetical order
if ((curr == 'a' && prev == 'z') ||
(curr - prev == 1))
i++;
else
break;
}
return i - 1;
}
// Function to return the length of the longest
// sub-string of consecutive characters from str
static int largestSubStr(String str, int n)
{
int len = 0;
int i = 0;
while (i < n) {
// Valid sub-string exists from index
// i to end
int end = getEndingIndex(str, n, i);
// Update the length
len = Math.max(end - i + 1, len);
i = end + 1;
}
return len;
}
// Driver code
public static void main(String args[])
{
String str = "abcabcdefabc";
int n = str.length();
System.out.print(largestSubStr(str, n));
}
}
Python3
# Python3 implementation of the approach
# Function to return the ending index for the
# largest valid sub-str1ing starting from index i
def getEndingIndex(str1, n, i):
i += 1
while (i < n):
curr = str1[i]
prev = str1[i - 1]
# If the current character appears after
# the previous character according to
# the given circular alphabetical order
if ((curr == 'a' and prev == 'z') or
(ord(curr) - ord(prev) == 1)):
i += 1
else:
break
return i - 1
# Function to return the length of the
# longest sub-str1ing of consecutive
# characters from str1
def largestSubstr1(str1, n):
Len = 0
i = 0
while (i < n):
# Valid sub-str1ing exists from
# index i to end
end = getEndingIndex(str1, n, i)
# Update the Length
Len = max(end - i + 1, Len)
i = end + 1
return Len
# Driver code
str1 = "abcabcdefabc"
n = len(str1)
print(largestSubstr1(str1, n))
# This code is contributed by
# Mohit Kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the ending index for the
// largest valid sub-string starting from index i
static int getEndingIndex(string str, int n, int i)
{
i++;
while (i < n)
{
char curr = str[i];
char prev = str[i - 1];
// If the current character appears after
// the previous character according to
// the given circular alphabetical order
if ((curr == 'a' && prev == 'z') ||
(curr - prev == 1))
i++;
else
break;
}
return i - 1;
}
// Function to return the length of the longest
// sub-string of consecutive characters from str
static int largestSubStr(string str, int n)
{
int len = 0;
int i = 0;
while (i < n)
{
// Valid sub-string exists from index
// i to end
int end = getEndingIndex(str, n, i);
// Update the length
len = Math.Max(end - i + 1, len);
i = end + 1;
}
return len;
}
// Driver code
public static void Main()
{
string str = "abcabcdefabc";
int n = str.Length;
Console.Write(largestSubStr(str, n));
}
}
// This code is contributed
// by Akanksha Rai
PHP
输出:
6
时间复杂度: O(n),其中n是输入字符串的长度。请注意,我们会逐步增加索引。