-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind_palindrome.cpp
More file actions
82 lines (67 loc) · 1.85 KB
/
find_palindrome.cpp
File metadata and controls
82 lines (67 loc) · 1.85 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Created by Mayank Parasar on 2020-03-28.
//
/*
Given a string, determine if there is a way to arrange the string such that the string is a palindrome.
If such arrangement exists, return a palindrome (There could be many arrangements). Otherwise return None.
Here's some starter code:
def find_palindrome(s):
# Fill this in.
print(find_palindrome('momom'))
# momom
*/
#include <iostream>
#include <map>
#include <string>
#include <deque>
using namespace std;
string find_palindrome(string str) {
map<char, int> mymap;
for(auto i : str) {
mymap[i]++;
}
// the nap is populated now...
int counter = 0; // to count the uneven occurance of a character
for(auto i : mymap) {
if(i.second % 2 == 1) {
counter++;
}
}
deque<char> word;
if(counter >= 2)
return "None";
else {
// make a palindrome string here from the map
// use a deque for this
for(auto i : mymap) {
if(i.second % 2 == 1) {
word.push_back(i.first);
mymap[i.first]--;
}
}
for(auto i : mymap) {
cout << i.first << ": ";
cout << i.second << endl;
}
// now populate rest of the word
for(auto i : mymap) {
for(int kk = i.second; kk > 0; kk -= 2) {
word.push_front(i.first);
word.push_back(i.first);
}
}
}
// now here palindrome word is compelete in deque data structure
// convert it into a string...
std::string palindrome(word.begin(), word.end());
return palindrome;
}
int main() {
std::string str = "mayank";
std::string str1 = "momom";
std::string str2 = "geeksogeeks";
std::string str3 = "mayank";
std::string str4 = "mayank";
cout << find_palindrome(str2);
return 0;
}