Katamaran
BAN USER
Comments (2)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
using namespace std;
void printPalindromePairs(string words[], unsigned int size) {
unordered_map<string, int> wordMap;
for (unsigned int i = 0; i < size; i++) {
string word = words[i];
reverse(word.begin(), word.end());
unordered_map<string, int>::iterator it = wordMap.find(word);
if (it == wordMap.end()) {
wordMap[words[i]] = i;
}
else {
cout << "FOUND : " << words[i] << ", " << word << endl;
wordMap.erase(it);
}
}
}
int main() {
string words[] = {"bat", "cat", "ami", "tac", "tab"};
printPalindromePairs(words, 5);
return 0;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Open Chat in New Window
Open Chat in New Window
I think this wont work in all cases. Consider "bcabc" - The algorithm will give us "bac", where as the right solution is "abc".
- Katamaran September 14, 2015