-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-034.cpp
57 lines (50 loc) · 1.58 KB
/
Day-034.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
48
49
50
51
52
53
54
55
56
57
/*
This problem was asked by Quora.
Given a string, find the palindrome that can be made by inserting the fewest
number of characters as possible anywhere in the word.
If there is more than one palindrome of minimum length that can be made,
return the lexicographically earliest one (the first one alphabetically).
For example, given the string "race", you should return "ecarace",
since we can add three letters to it (which is the smallest amount to make a palindrome).
There are seven other palindromes that can be made from "race" by adding three letters, but "ecarace" comes first alphabetically.
As another example, given the string "google", you should return "elgoogle".
*/
#include <bits/stdc++.h>
using namespace std;
bool palin(string s){
int i=0 , j=(int)s.size()-1;
while(i<j){
if(s[i]!=s[j]){
return false;
}
i++;j--;
}
return true;
}
string get_palin(string s){
if(palin(s)){
return s;
}
if(s[0]==s[s.size()-1]){
return s[0]+get_palin(s.substr(1,s.size()-1))+s[s.size()-1];
}else{
string left = s[0]+get_palin(s.substr(1,s.size()))+s[0];
string right = s[s.size()-1]+get_palin(s.substr(0,s.size()-1))+s[s.size()-1];
if(left.size()>right.size()){
return right;
}else if(left.size()<right.size()){
return left;
}
if(left>right){
return right;
}else{
return left;
}
}
}
int main(void){
string s , rev;
cin >> s;
cout<<get_palin(s)<<'\n';
return 0;
}