forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.cpp
47 lines (42 loc) · 1.16 KB
/
sol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <vector>
#include <iostream>
#include <cstring>
bool isPalindrome(std::string str){
int n =str.length();
int m = n/2;
for (int i=0; i<m; i++){
if (str.at(i)!=str.at(n-i-1)) return false;
}
return true;
}
std::vector<std::pair<int, int>> allPalindromes(std::vector<std::string> words){
std::vector<std::pair<int, int>> pairs;
for (int i=0; i<words.size(); i++){
for (int j = 0; j<words.size(); j++){
if (i==j){
continue;
}
std::string word = words[i]+words[j];
if (isPalindrome(word)){
pairs.push_back(std::make_pair(i, j));
}
}
}
return pairs;
}
void test(){
std::vector<std::string> words = {"code", "edoc", "da", "d"};
std::vector<std::pair<int, int>> pairs = allPalindromes(words);
std::cout<<'[';
for (auto p: pairs) std::cout<<'('<<p.first<<' '<<p.second<<"), ";
std::cout<<']'<<'\n';
}
int main(){
test();
// std::string x = "aqua";
// std::string y = "man";
// std::vector<std::string> v = {"aagaa", "agag", "aglga", "aglollga", "agllga"};
// std::cout<<x+y<<'\n';
// for (auto i: v) std::cout<<i<<" palindrome? "<<std::boolalpha<<isPalindrome(i)<<'\n';
return 0;
}