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
- Minimum length of substring whose rotation generates a palindromic substring
- First string from the given array whose reverse is also present in the same array
- Reverse all the word in a String represented as a Linked List
- 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
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- C program to find and replace a word in a File by another given word
- Find the word from a given sentence having given word as prefix
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Minimum changes required to make first string substring of second string
- Reverse each word in a linked list node
- Get the first letter of each word in a string using regex in Java
- Find the first repeated word in a string
- Find the first maximum length even word from a string
- Print the first and last character of each word in a String
- Remove the first and last character of each word in a string
- Capitalize 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.