检查字符串是否在末端有一个可逆的相等子串
给定一个由N个字符组成的字符串S ,任务是检查该字符串是否具有从开头到结尾的可逆相等子字符串。如果是,则打印True ,然后打印符合给定条件的最长子字符串,否则打印False。
例子:
Input: S = “abca”
Output:
True
a
Explanation:
The substring “a” is only the longest substring that satisfy the given criteria. Therefore, print a.
Input: S = “acdfbcdca”
Output:
True
acd
Explanation:
The substring “acd” is only the longest substring that satisfy the given criteria. Therefore, print acd.
Input: S = “abcdcb”
Output: False
方法:给定的问题可以通过使用双指针方法来解决。按照以下步骤解决给定问题:
- 初始化一个字符串,比如ans ,它存储满足给定条件的结果字符串。
- 初始化两个变量,比如i和j分别为0和(N – 1) 。
- 迭代一个循环,直到j非负且 f字符S[i]和S[j]相同,然后只需在变量ans中添加字符S[i]并将i的值增加1并减少j的值乘以1 。否则,打破循环。
- 完成上述步骤后,如果字符串ans为空,则打印False 。否则,打印True然后打印字符串ans作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print longest substring
// that appears at beginning of string
// and also at end in reverse order
void commonSubstring(string s)
{
int n = s.size();
int i = 0;
int j = n - 1;
// Stores the resultant string
string ans = "";
while (j >= 0) {
// If the characters are same
if (s[i] == s[j]) {
ans += s[i];
i++;
j--;
}
// Otherwise, break
else {
break;
}
}
// If the string can't be formed
if (ans.size() == 0)
cout << "False";
// Otherwise print resultant string
else {
cout << "True \n"
<< ans;
}
}
// Driver Code
int main()
{
string S = "abca";
commonSubstring(S);
return 0;
}
Java
// Java program for the above approach
public class GFG
{
// Function to print longest substring
// that appears at beginning of string
// and also at end in reverse order
static void commonSubstring(String s)
{
int n = s.length();
int i = 0;
int j = n - 1;
// Stores the resultant string
String ans = "";
while (j >= 0) {
// If the characters are same
if (s.charAt(i) == s.charAt(j)) {
ans += s.charAt(i);
i++;
j--;
}
// Otherwise, break
else {
break;
}
}
// If the string can't be formed
if (ans.length() == 0)
System.out.println("False");
// Otherwise print resultant string
else {
System.out.println("True ");
System.out.println(ans);
}
}
// Driver Code
public static void main(String []args)
{
String S = "abca";
commonSubstring(S);
}
}
// This code is contributed by AnkThon
Python3
# python program for the above approach
# Function to print longest substring
# that appears at beginning of string
# and also at end in reverse order
def commonSubstring(s):
n = len(s)
i = 0
j = n - 1
# Stores the resultant string
ans = ""
while (j >= 0):
# // If the characters are same
if (s[i] == s[j]):
ans += s[i]
i = i + 1
j = j - 1
# Otherwise, break
else:
break
# If the string can't be formed
if (len(ans) == 0):
print("False")
# Otherwise print resultant string
else:
print("True")
print(ans)
# Driver Code
if __name__ == "__main__":
S = "abca"
commonSubstring(S)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to print longest substring
// that appears at beginning of string
// and also at end in reverse order
static void commonSubstring(string s)
{
int n = s.Length;
int i = 0;
int j = n - 1;
// Stores the resultant string
string ans = "";
while (j >= 0) {
// If the characters are same
if (s[i] == s[j]) {
ans += s[i];
i++;
j--;
}
// Otherwise, break
else {
break;
}
}
// If the string can't be formed
if (ans.Length == 0)
Console.WriteLine("False");
// Otherwise print resultant string
else {
Console.WriteLine("True ");
Console.WriteLine(ans);
}
}
// Driver Code
public static void Main()
{
string S = "abca";
commonSubstring(S);
}
}
// This code is contributed by ukasp.
Javascript
输出
a
时间复杂度: O(N)
辅助空间: O(1)