给定一个由N 个字符和正整数K组成的字符串S ,任务是检查是否存在(K + 1) 个字符串,即A 1 , A 2 , A 3 , …, A K , A (K + 1)使得字符串A 1 , A 2 , A 3 , …, A K和A (K + 1)的串联以及每个字符串A K , A (K – 1) , A (K – 2) , …, A 1 , A 0是字符串S 。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
Input: S = “qwqwq”, K = 1
Output: Yes
Explanation:
Consider the string A1 as “qw”, and A2 as “q”. Now the concatenation of A1, A2, reverse of A1 is “qwqwq”, which is the same as the given string S.
Input: S = “qwqwa”, K = 2
Output: No
方法:给定的问题可以根据观察来解决,即对于满足给定条件的字符串S ,前K 个字符必须等于给定字符串的最后K 个字符。请按照以下步骤解决问题:
- 如果 ( 2*K + 1)的值大于N ,则打印“No”并从函数返回。
- 否则,将大小为K的前缀即S[0, …, K] 存储在字符串A 中,并将大小为K的后缀即S[N – K, …, N – 1] 存储在字符串B 中。
- 反转字符串B并检查A是否等于B。如果发现是真的,则打印“是” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the string S
// can be obtained by (K + 1) non-empty
// substrings whose concatenation and
// concatenation of the reverse
// of these K strings
void checkString(string s, int k)
{
// Stores the size of the string
int n = s.size();
// If n is less than 2*k+1
if (2 * k + 1 > n) {
cout << "No";
return;
}
// Stores the first K characters
string a = s.substr(0, k);
// Stores the last K characters
string b = s.substr(n - k, k);
// Reverse the string
reverse(b.begin(), b.end());
// If both the strings are equal
if (a == b)
cout << "Yes";
else
cout << "No";
}
// Driver Code
int main()
{
string S = "qwqwq";
int K = 1;
checkString(S, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to check if the string S
// can be obtained by (K + 1) non-empty
// substrings whose concatenation and
// concatenation of the reverse
// of these K strings
static void checkString(String s, int k)
{
// Stores the size of the string
int n = s.length();
// If n is less than 2*k+1
if (2 * k + 1 > n) {
System.out.println("No");
return;
}
// Stores the first K characters
String a = s.substring(0, k);
// Stores the last K characters
String b = s.substring(n - k, n);
// Reverse the string
StringBuffer str = new StringBuffer(b);
// To reverse the string
str.reverse();
b = str.toString();
// If both the strings are equal
if (a.equals(b))
System.out.println("Yes");
else
System.out.println("No");
}
// Driver Code
public static void main (String[] args)
{
String S = "qwqwq";
int K = 1;
checkString(S, K);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# Function to check if the S
# can be obtained by (K + 1) non-empty
# substrings whose concatenation and
# concatenation of the reverse
# of these K strings
def checkString(s, k):
# Stores the size of the string
n = len(s)
# If n is less than 2*k+1
if (2 * k + 1 > n):
print("No")
return
# Stores the first K characters
a = s[0:k]
# Stores the last K characters
b = s[n - k:n]
# Reverse the string
b = b[::-1]
# If both the strings are equal
if (a == b):
print("Yes")
else:
print("No")
# Driver Code
if __name__ == '__main__':
S = "qwqwq"
K = 1
checkString(S, K)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG {
// Function to check if the string S
// can be obtained by (K + 1) non-empty
// substrings whose concatenation and
// concatenation of the reverse
// of these K strings
static void checkString(string s, int k)
{
// Stores the size of the string
int n = s.Length;
// If n is less than 2*k+1
if (2 * k + 1 > n) {
Console.Write("No");
return;
}
// Stores the first K characters
string a = s.Substring(0, k);
// Stores the last K characters
string b = s.Substring(n - k, k);
// Reverse the string
char[] arr = b.ToCharArray();
Array.Reverse(arr);
b = new String(arr);
// If both the strings are equal
if (a == b)
Console.Write("Yes");
else
Console.Write("No");
}
// Driver Code
public static void Main()
{
string S = "qwqwq";
int K = 1;
checkString(S, K);
}
}
// This code is contributed by ukasp.
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live