📜  完美的可逆字符串

📅  最后修改于: 2022-05-13 01:57:07.710000             🧑  作者: Mango

完美的可逆字符串

你得到一个字符串'str',任务是检查'str'的所有可能子字符串的反转是否存在于'str'中。

例子:

Input : str = "ab"
Output: "NO"
// all substrings are "a","b","ab" but reverse
// of "ab" is not present in str

Input : str = "aba"
Output: "YES"

Input : str = "abab"
Output: "NO"
// All substrings are "a", "b", "a", "b", "ab", 
// "ba", "ab", "aba", "bab", "abab" but reverse of
// "abab" is not present in str

这个问题的一个简单解决方案是生成'st'的所有可能的子字符串,并检查它们的反向是否线性地存在于'str'中。
这个问题的一个有效解决方案是基于这样一个事实,即当且仅当整个字符串'str' 是回文时,'str' 的所有子字符串的反转将存在于 'str' 中。我们可以通过考虑整个字符串来证明这一事实,只有当它是回文时才会存在它的反转。如果一个字符串是回文,那么所有子字符串的所有反转都存在。

下面是上述想法的实现。

C++
// C++ program to check if a string is perfect
// reversible or nor
#include
using namespace std;
 
// This function basically checks if string is
// palindrome or not
bool isReversible(string str)
{
     int i = 0, j = str.length()-1;
 
     // iterate from left and right
     while (i < j)
     {
        if (str[i] != str[j])
            return false;
        i++;
        j--;
     }
     return true;
}
 
// Driver program to run the case
int main()
{
  string str="aba";
  if (isReversible(str))
      cout << "YES";
  else
      cout << "NO";
  return 0;
}


Java
// Java program to check
// if a string is perfect
// reversible or nor
import java.io.*;
 
class GFG
{
 
// This function basically
// checks if string is
// palindrome or not
static boolean isReversible(String str)
{
    int i = 0, j = str.length() - 1;
 
    // iterate from
    // left and right
    while (i < j)
    {
        if (str.charAt(i) != str.charAt(j))
            return false;
        i++;
        j--;
    }
    return true;
}
 
// Driver Code
public static void main (String[] args)
{
    String str = "aba";
    if (isReversible(str))
        System.out.print("YES");
    else
        System.out.print( "NO");
}
}
 
// This code is contributed
// by anuj_67.


Python3
# Python3 program to check if
# a string is perfect reversible or not
 
# This function basically checks
# if string is palindrome or not
def isReversible(str):
    i = 0; j = len(str) - 1;
 
    # iterate from left and right
    while (i < j):
        if (str[i] != str[j]):
            return False;
        i += 1;
        j -= 1;
    return True;
 
# Driver Code
str = "aba";
if (isReversible(str)):
    print("YES");
else:
    print("NO");
     
# This code is contributed by Princi Singh


C#
// C# program to check if a string
// is perfect reversible or nor
using System;
 
class GFG
{
 
// This function basically checks if
// string is palindrome or not
public static bool isReversible(string str)
{
    int i = 0, j = str.Length - 1;
 
    // iterate from left and right
    while (i < j)
    {
        if (str[i] != str[j])
        {
            return false;
        }
        i++;
        j--;
    }
    return true;
}
 
// Driver Code
public static void Main(string[] args)
{
    string str = "aba";
    if (isReversible(str))
    {
        Console.Write("YES");
    }
    else
    {
        Console.Write("NO");
    }
}
}
 
// This code is contributed
// by anuj_67


Javascript


输出:

YES