//the task is to find the pairs ina string array of unique words such that if we combine them they form aplalindrome
#include
#include<unordered_map>
using namespace std;
bool palindrome(string arr[],int i,int j){
while(i<j){
if(arr[i]!=arr[j])
return false;
i++;
j–;
}
return true;
}
string find_reverse(string str){
int len=str.length();
string rev="";
for(int i=len-1;i>=0;i–)
rev[len-1-i]=str[i];
cout<<rev<<endl;
return rev;
}
void palindromepairs(string arr[],int n){
//storing the reverse of the strings in the hash map
unordered_map<string,int> rev;
for(int i=0;i<n;i++){
cout<<arr[i]<<endl;
string str=find_reverse(arr[i]);
cout<<str<<endl;
rev[str]=i;
}
//iterating over each string and finding whetger its reverse exists
for(int i=0;i<n;i++){
int len=arr[i].size();
cout<<len<<endl<<endl;
string sub;
//iterating over esch substring of arr[i] strating from 0 and checking whether its reverse is present or not
for(int j=0;j<len;j++){
sub[j]=arr[i][j];
cout<<sub<<endl;
if(rev.count(sub)){
if(palindrome(arr,j+1,n-1))
cout<<"[ “<<i<<” “<< rev[sub]<<” ]"<<endl;
}
}
//in the above case the arr[i] is always a prefix it does not consider the acses where arr[i] is a suffix
for(int j=len-1;j>=0;j--){
sub[len-1-j]=arr[i][j];
if(rev.count(sub)){
if(palindrome(arr,0,j-1))
cout<<"[ "<<rev[sub]<<" "<< i<<" ]"<<",";
}
}
}
}
int main(){
int n;
cin>>n;
string * str=new string[n];
for(int i=0;i<n;i++)
cin>>str[i];
palindromepairs(str,n);
return 0;
}
what are the issues in this code?