Given a space-separated string str, the task is to find the first substring whose reverse is a word in the string. All characters of the string are lowercase English alphabets. The string ends with #. If no such substring is present return -1
Input: str = “mango is sweet when nam en tastes it#”
Explanation: Substring “man” is reversed to “nam” and it is a word in the given string
Input: str = “hello world#”
- Store all the words of the string in a map.
- Find all substrings of the string.
- For each substring check whether the reverse of the substring is a word of the string. If no such substring exists print -1.
Below is the implementation of the above approach.
- Reverse every word of the string except the first and the last character
- Reverse all the word in a String represented as a Linked List
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Reverse each word in a linked list node
- Substring Reverse Pattern
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Find length of longest subsequence of one string which is substring of another string
- String containing first letter of every word in a given string with spaces
- Most frequent word in first String which is not present in second String
- Minimum changes required to make first string substring of second string
- Queries to check if string B exists as substring in string A
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Length Of Last Word in a String
- Print the first and last character of each word in a String
- Print last character of each word in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.