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
Examples:
Input: str = “mango is sweet when nam en tastes it#”
Output: man
Explanation: Substring “man” is reversed to “nam” and it is a word in the given stringInput: str = “hello world#”
Output: -1
Approach:
- 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.
C++
// C++ program to find first substring #include <bits/stdc++.h> using namespace std; // Function to find first substring string first_substring(string s) { int n = s.length(), c = 0; string s1, s2; map<string, int> mpp; // Storing the words present in string for (int i = 0; i < n; i++) { if (s[i] == ' ' || s[i] == '#') { s1 = s.substr(c, i - c); mpp[s1] = 1; c = i + 1; } } // Calculating for all // possible valid substring. for (int i = 0; i < n; i++) { if (s[i] == ' ') { continue; } for (int j = 0; j < n; j++) { if (s[j] == ' ') { break; } s1 = s.substr(i, j - i + 1); s2 = s1; reverse(s1.begin(), s1.end()); if (mpp[s1]) { return s2; } } } return "-1"; } // Driver code int main() { string s, s1; s = "mango is sweet when nam en tastes it#"; s1 = first_substring(s); cout << s1 << "\n"; return 0; } |
Java
// Java program to find first subString import java.util.*; class GFG { // Function to find first subString static String first_subString(String s) { int n = s.length(), c = 0; String s1, s2; HashMap<String, Integer> mpp = new HashMap<String, Integer>(); // Storing the words present in String for (int i = 0; i < n; i++) { if (s.charAt(i) == ' ' || s.charAt(i) == '#') { s1 = s.substring(c, i); mpp.put(s1, 1); c = i + 1; } } // Calculating for all // possible valid subString. for (int i = 0; i < n; i++) { if (s.charAt(i) == ' ') { continue; } for (int j = 0; j < n; j++) { if (s.charAt(i) == ' ') { break; } s1 = s.substring(i, j - i + 1); s2 = s1; s1 = reverse(s1); if (mpp.containsKey(s1)) { return s2; } } } return "-1"; } static String reverse(String input) { char[] a = input.toCharArray(); int l, r = a.length - 1; for (l = 0; l < r; l++, r--) { char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.valueOf(a); } // Driver code public static void main(String[] args) { String s, s1; s = "mango is sweet when nam en tastes it#"; s1 = first_subString(s); System.out.print(s1+ "\n"); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to find first substring # Function to find first substring def first_substring(s) : n = len(s); c = 0; mpp = {}; # Storing the words present in string for i in range(n) : if (s[i] == ' ' or s[i] == '#') : s1 = s[c: i]; mpp[s1] = 1; c = i + 1; # Calculating for all # possible valid substring. for i in range(n) : if (s[i] == ' ') : continue; for j in range(n) : if (s[j] == ' ') : break; s1 = s[i : j + 1]; s2 = s1; s1 = s1[::-1]; if s1 in mpp : if mpp[s1] : return s2; return "-1"; # Driver code if __name__ == "__main__" : s = "mango is sweet when nam en tastes it#"; s1 = first_substring(s); print(s1); # This code is contributed by AnkitRai01 |
C#
// C# program to find first subString using System; using System.Collections.Generic; class GFG { // Function to find first subString static String first_subString(String s) { int n = s.Length, c = 0; String s1, s2; Dictionary<String, int> mpp = new Dictionary<String, int>(); // Storing the words present in String for (int i = 0; i < n; i++) { if (s[i] == ' ' || s[i] == '#') { s1 = s.Substring(c, i - c); mpp.Add(s1, 1); c = i + 1; } } // Calculating for all // possible valid subString. for (int i = 0; i < n; i++) { if (s[i] == ' ') { continue; } for (int j = 0; j < n; j++) { if (s[i] == ' ') { break; } s1 = s.Substring(i, j - i + 1); s2 = s1; s1 = reverse(s1); if (mpp.ContainsKey(s1)) { return s2; } } } return "-1"; } static String reverse(String input) { char[] a = input.ToCharArray(); int l, r = a.Length - 1; for (l = 0; l < r; l++, r--) { char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.Join("", a); } // Driver code public static void Main(String[] args) { String s, s1; s = "mango is sweet when nam en tastes it#"; s1 = first_subString(s); Console.Write(s1 + "\n"); } } // This code is contributed by Rajput-Ji |
man
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.
Recommended Posts:
- 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 contribute@geeksforgeeks.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.

