You are given a string ‘str’, the task is to check that reverses of all possible substrings of ‘str’ are present in ‘str’ or not.
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
A simple solution for this problem is to generate all possible substrings of ‘st’ and check if their reverse exist in the ‘str’ linearly.
An efficient solution for this problem is based on the fact that reverse of all substrings of ‘str’ will exist in ‘str’ if and only if the entire string ‘str’ is palindrome. We can justify this fact by considering the whole string, a reverse of it will exist only if it is palindrome. And if a string is palindrome, then all reverse of all substrings exist.
Below is implementation of above idea.
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Perfect Cube String
- Perfect Square String
- String slicing in Python to check if a string can become empty by recursive deletion
- String Range Queries to find the number of subsets equal to a given String
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Minimal moves to form a string by adding characters or appending string itself
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Transform string str1 into str2 by taking characters from string str3
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Find the count of palindromic sub-string of a string in its sorted form
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find the character in first string that is present at minimum index in second string
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string can be converted to another string by replacing vowels and consonants