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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Reverse each word in a linked list node
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Substring Reverse Pattern
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Most frequent word in first String which is not present in second String
- String containing first letter of every word in a given string with spaces
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Find length of longest subsequence of one string which is substring of another string
- Queries to check if string B exists as substring in string A
- Minimum changes required to make first string substring of second string
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Check if the given string is shuffled substring of another string
- Length Of Last Word in a String
- Check if a given string is a Reverse Bitonic String or not
- Calculate the frequency of each word in the given string
- Count occurrences of a word in string
- Print last character of each word in a string
- Print the first and 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 email@example.com. 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.