
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
First string from the given array whose reverse is also present in the same array in C++
In this problem, we are given an array of string str[] of size N. Our task is to create a program for finding the first string from the given array whose reverse is also present in the same array.
Let's take an example to understand the problem,
Input: str[] = ["python", "program", "C#", "language", "#C"] Output: C#
Solution Approach
One way to solve the problem is by directly traversing each element of the string array and checking for revere of the string in the remaining array. Return the string if the reverse is found. If the whole array is traversed and no string with the reverse present is found then return -1.
Example
Program to illustrate the working of our solution
#include<iostream> #include<string.h> using namespace std; bool checkStringRev(string s1, string s2) { if (s1.length() != s2.length()) return false; int len = s1.length(); for (int i = 0; i < len; i++) if (s1[i] != s2[len - i - 1]) return false; return true; } string checkRevStringArr(string strArr[], int n){ for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (checkStringRev(strArr[i], strArr[j])) return strArr[i]; return "-1"; } int main(){ string strArr[] = { "python", "program", "C#", "language", "#C" }; int n = sizeof(strArr)/sizeof(strArr[0]); cout<<"The string from the array whose reverse is present in the array is " <<checkRevStringArr(strArr, n); }
Output
The string from the array whose reverse is present in the array is C#
Another method that can solve the problem in linear time i.e. in single traversal is by using a hashmap. We will be storing each word in the hashmap. If any string has its reverse present in the hashmap then the reverse string from hashmap is our result. And if the whole array does not contain any such string, return -1.
Example
Program to illustrate the working of our solution
#include<bits/stdc++.h> using namespace std; string checkRevStringArr(string strArr[], int length){ map<string,bool> stringHashMap; for(int i = 0; i < length; i++) { string str = strArr[i]; reverse(str.begin(),str.end()); if (stringHashMap.find(str) != stringHashMap.end() and stringHashMap[str]) return str; else stringHashMap[strArr[i]] = true; } return "-1"; } int main(){ string strArr[] = { "python", "program", "C#", "language", "#C" }; int n = sizeof(strArr)/sizeof(strArr[0]); cout<<"The string from the array whose reverse is present in the array is "<<checkRevStringArr(strArr, n); }
Output
The string from the array whose reverse is present in the array is C#
- Related Articles
- Count number of subsets whose median is also present in the same subset in C++
- Maximum length of the sub-array whose first and last elements are same in C++
- Count pairs with average present in the same array in C++
- Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
- Maximum array from two given arrays keeping order same in C++
- Print the longest prefix of the given string which is also the suffix of the same string in C Program.
- C++ Program to get the first given number of items from the array
- Program to find array of length k from given array whose unfairness is minimum in python
- Reverse an array in C++
- Count elements present in first array but not in second in C++
- Maximize the median of the given array after adding K elements to the same array in C++
- Given an array of integers return positives, whose equivalent negatives present in it in JavaScript
- Find the Initial Array from given array after range sum queries in C++
- Write a program to reverse an array or string in C++
- Elements present in first array and not in second using STL in C++
