给定三个字符串S 、 S1和S2 ,任务是检查以S1和S2开头和结尾的子字符串的数量是否等于以S2和S1开头和结尾的子字符串的数量。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
Input: S = “helloworldworldhelloworld”, S1 = “hello”, S2 = “world”
Output: No
Explanation:
The substrings that starts with S1 (= “hello”) and ends with S2 (= “world”) are {“helloworld”, “helloworldworld”, “helloworldworldhelloworld”, “helloworld”}.
Therefore, the total count is 4.
The substrings that starts with S2 (= “world”) and ends with S1(= “hello”) are {“worldworldhello”, “worldhello”}.
Therefore, the total count is 2.
Therefore, the above two counts are not the same.
Input: S = “opencloseopencloseopen”, S1 = “open”, S2 = “close”
Output: Yes
处理方法:按照以下步骤解决问题:
- 将字符串S 、 S1和S2的长度分别存储在变量中,比如N 、 N1和N2 。
- 初始化一个变量,比如count ,以存储子字符串S1在字符串S中出现的次数。
- 初始化一个变量,比如ans ,以存储分别以S1和S2开头和结尾的子字符串。
- 遍历给定的字符串S并执行以下步骤:
- 将字符串S的子字符串从大小为N1的索引i开始存储在字符串prefix 中。
- 将字符串S的子字符串存储在字符串suffix 中,从大小为N2的索引i开始。
- 如果前缀与字符串S1相同,则将count的值增加1 。
- 如果后缀与字符串S2相同,则将ans的值增加count 。
- 使用上述步骤,找出分别以S2和S1开头和结尾的子串的数量。将获得的计数存储在变量 say ans2 中。
- 如果发现ans和ans2的值相等,则打印“Yes” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count number of
// substrings that starts with
// string S1 and ends with string S2
int countSubstrings(string S, string S1,
string S2)
{
// Stores the length of each string
int N = S.length();
int N1 = S1.length();
int N2 = S2.length();
// Stores the count of prefixes
// as S1 and suffixes as S2
int count = 0, totalcount = 0;
// Traverse string S
for (int i = 0; i < N; i++) {
// Find the prefix at index i
string prefix = S.substr(i, N1);
// Find the suffix at index i
string suffix = S.substr(i, N2);
// If the prefix is S1
if (S1 == prefix)
count++;
// If the suffix is S2
if (S2 == suffix)
totalcount += count;
}
// Return the count of substrings
return totalcount;
}
// Function to check if the number of
// substrings starts with S1 and ends
// with S2 and vice-versa is same or not
void checkSubstrings(string S, string S1,
string S2)
{
// Count the number of substrings
int x = countSubstrings(S, S1, S2);
int y = countSubstrings(S, S2, S1);
// Print the result
if (x == y)
cout << "Yes";
else
cout << "No";
}
// Driver Code
int main()
{
string S = "opencloseopencloseopen";
string S1 = "open";
string S2 = "close";
checkSubstrings(S, S1, S2);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to count number of
// substrings that starts with
// string S1 and ends with string S2
static int countSubstrings(String S, String S1,
String S2)
{
// Stores the length of each string
int N = S.length();
int N1 = S1.length();
int N2 = S2.length();
// Stores the count of prefixes
// as S1 and suffixes as S2
int count = 0, totalcount = 0;
// Traverse string S
for(int i = 0; i < N; i++)
{
// Find the prefix at index i
String prefix = S.substring(
i, (i + N1 < N) ? (i + N1) : N);
// Find the suffix at index i
String suffix = S.substring(
i, (i + N2 < N) ? (i + N2) : N);
// If the prefix is S1
if (S1.equals(prefix))
count++;
// If the suffix is S2
if (S2.equals(suffix))
totalcount += count;
}
// Return the count of substrings
return totalcount;
}
// Function to check if the number of
// substrings starts with S1 and ends
// with S2 and vice-versa is same or not
static void checkSubstrings(String S, String S1,
String S2)
{
// Count the number of substrings
int x = countSubstrings(S, S1, S2);
int y = countSubstrings(S, S2, S1);
// Print the result
if (x == y)
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main(String[] args)
{
String S = "opencloseopencloseopen";
String S1 = "open";
String S2 = "close";
checkSubstrings(S, S1, S2);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to count number of
# substrings that starts with
# string S1 and ends with string S2
def countSubstrings(S, S1, S2):
# Stores the length of each string
N = len(S)
N1 = len(S1)
N2 = len(S2)
# Stores the count of prefixes
# as S1 and suffixes as S2
count = 0
totalcount = 0
# Traverse string S
for i in range(N):
# Find the prefix at index i
prefix = S[i: (i + N1) if (i + N1 < N) else N]
# Find the suffix at index i
suffix = S[i: (i + N2) if (i + N2 < N) else N]
# If the prefix is S1
if S1 == prefix:
count += 1
# If the suffix is S2
if S2 == suffix:
totalcount += count
# Return the count of substrings
return totalcount
# Function to check if the number of
# substrings starts with S1 and ends
# with S2 and vice-versa is same or not
def checkSubstrings(S, S1, S2):
x = countSubstrings(S, S1, S2)
y = countSubstrings(S, S2, S1)
if x == y:
print("Yes")
else:
print("No")
# Driver code
S = "opencloseopencloseopen"
S1 = "open"
S2 = "close"
checkSubstrings(S, S1, S2)
# This code is contributed by abhinavjain194
C#
// C# program for the above approach
using System;
class GFG{
// Function to count number of
// substrings that starts with
// string S1 and ends with string S2
static int countSubstrings(string S, string S1,
string S2)
{
// Stores the length of each string
int N = S.Length;
int N1 = S1.Length;
int N2 = S2.Length;
// Stores the count of prefixes
// as S1 and suffixes as S2
int count = 0, totalcount = 0;
// Traverse string S
for(int i = 0; i < N; i++)
{
// Find the prefix at index i
String prefix = S.Substring(
i, (i + N1 < N) ? N1 : (N - i));
// Find the suffix at index i
String suffix = S.Substring(
i, (i + N2 < N) ? N2 : (N - i));
// If the prefix is S1
if (S1.Equals(prefix))
count++;
// If the suffix is S2
if (S2.Equals(suffix))
totalcount += count;
}
// Return the count of substrings
return totalcount;
}
// Function to check if the number of
// substrings starts with S1 and ends
// with S2 and vice-versa is same or not
static void checkSubstrings(string S, string S1,
string S2)
{
// Count the number of substrings
int x = countSubstrings(S, S1, S2);
int y = countSubstrings(S, S2, S1);
// Print the result
if (x == y)
Console.Write("Yes");
else
Console.Write("No");
}
// Driver code
static void Main()
{
string S = "opencloseopencloseopen";
string S1 = "open";
string S2 = "close";
checkSubstrings(S, S1, S2);
}
}
// This code is contributed by abhinavjain194
Javascript
Yes
时间复杂度: O(N * (N1 + N2)),其中 N、N1 和 N2 分别是字符串S、S1 和 S2 的长度。
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live