给定的字符串S的是字符串“ABCDEFGHIJKLMNOPQRSTUVWXYZ”的无限环绕字符串,任务是计数字符串P的唯一非空的子串的数目是存在于第
例子:
Input: S = “zab”
Output: 6
Explanation: All possible substrings are “z”, “a”, “b”, “za”, “ab”, “zab”.
Input: S = “cac”
Output: 2
Explanation: All possible substrings are “a” and “c” only.
处理方法:按照以下步骤解决问题
- 遍历字符串的每个字符
- 初始化大小为26的辅助数组arr[] ,以存储从字符串P 的每个字符开始的字符串S 中存在的子字符串的当前长度。
- 初始化一个变量,比如curLen ,如果当前字符不是前一个子字符串的一部分,它存储P 中存在的子字符串的长度,包括当前字符。
- 初始化一个变量,比如ans ,以存储S 中存在的p的非空子串的唯一计数。
- 遍历字符串并检查以下两种情况的字符:
- 检查当前字符可以与前一个子串相加形成所需的子串。
- 如果(curLen + 1)大于arr[curr] ,则将 curLen和arr[curr]的差值添加到ans 中,以避免重复子串。
- 打印ans的值。
下面是上述方法的实现:
C++
// C++ program for
// the above approach
#include
using namespace std;
// Function to find the count of
// non-empty substrings of p present in s
int findSubstringInWraproundString(string p)
{
// Stores the required answer
int ans = 0;
// Stores the length of
// substring present in p
int curLen = 0;
// Stores the current length
// of substring that is
// present in string s starting
// from each character of p
int arr[26] = { 0 };
// Iterate over the characters of the string
for (int i = 0; i < (int)p.length(); i++) {
int curr = p[i] - 'a';
// Check if the current character
// can be added with previous substring
// to form the required substring
if (i > 0
&& (p[i - 1]
!= ((curr + 26 - 1) % 26 + 'a'))) {
curLen = 0;
}
// Increment current length
curLen++;
if (curLen > arr[curr]) {
// To avoid repetition
ans += (curLen - arr[curr]);
// Update arr[cur]
arr[curr] = curLen;
}
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
string p = "zab";
// Function call to find the
// count of non-empty substrings
// of p present in s
findSubstringInWraproundString(p);
return 0;
}
Java
import java.util.*;
class GFG
{
// Function to find the count of
// non-empty substrings of p present in s
static void findSubstringInWraproundString(String p)
{
// Stores the required answer
int ans = 0;
// Stores the length of
// substring present in p
int curLen = 0;
// Stores the current length
// of substring that is
// present in string s starting
// from each character of p
int arr[] = new int[26];
// Iterate over the characters of the string
for (int i = 0; i < p.length(); i++)
{
int curr = p.charAt(i) - 'a';
// Check if the current character
// can be added with previous substring
// to form the required substring
if (i > 0
&& (p.charAt(i - 1)
!= ((curr + 26 - 1) % 26 + 'a')))
{
curLen = 0;
}
// Increment current length
curLen++;
if (curLen > arr[curr])
{
// To avoid repetition
ans += (curLen - arr[curr]);
// Update arr[cur]
arr[curr] = curLen;
}
}
// Print the answer
System.out.println(ans);
}
// Driver Code
public static void main(String args[])
{
String p = "zab";
// Function call to find the
// count of non-empty substrings
// of p present in s
findSubstringInWraproundString(p);
}
}
// This code is contributed by hemanth gadarla
Python3
# Python3 program for
# the above approach
# Function to find the count of
# non-empty substrings of p present in s
def findSubstringInWraproundString(p) :
# Stores the required answer
ans = 0
# Stores the length of
# substring present in p
curLen = 0
# Stores the current length
# of substring that is
# present in string s starting
# from each character of p
arr = [0]*26
# Iterate over the characters of the string
for i in range(0, len(p)) :
curr = ord(p[i]) - ord('a')
# Check if the current character
# can be added with previous substring
# to form the required substring
if (i > 0 and (ord(p[i - 1]) != ((curr + 26 - 1) % 26 + ord('a')))) :
curLen = 0
# Increment current length
curLen += 1
if (curLen > arr[curr]) :
# To avoid repetition
ans += (curLen - arr[curr])
# Update arr[cur]
arr[curr] = curLen
# Print the answer
print(ans)
p = "zab"
# Function call to find the
# count of non-empty substrings
# of p present in s
findSubstringInWraproundString(p)
# This code is contributed by divyeshrabadiya07.
C#
// C# program for
// the above approach
using System;
class GFG
{
// Function to find the count of
// non-empty substrings of p present in s
static void findSubstringInWraproundString(string p)
{
// Stores the required answer
int ans = 0;
// Stores the length of
// substring present in p
int curLen = 0;
// Stores the current length
// of substring that is
// present in string s starting
// from each character of p
int[] arr = new int[26];
// Iterate over the characters of the string
for (int i = 0; i < (int)p.Length; i++)
{
int curr = p[i] - 'a';
// Check if the current character
// can be added with previous substring
// to form the required substring
if (i > 0 && (p[i - 1] != ((curr + 26 - 1) % 26 + 'a')))
{
curLen = 0;
}
// Increment current length
curLen++;
if (curLen > arr[curr])
{
// To avoid repetition
ans += (curLen - arr[curr]);
// Update arr[cur]
arr[curr] = curLen;
}
}
// Print the answer
Console.Write(ans);
}
// Driver code
static void Main()
{
string p = "zab";
// Function call to find the
// count of non-empty substrings
// of p present in s
findSubstringInWraproundString(p);
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
6
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live