Skip to content

Commit b5a8c2f

Browse files
1 parent 9421a86 commit b5a8c2f

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

LongestPalindromicSubstring

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <string>
2+
3+
class Solution {
4+
public:
5+
/*
6+
bool isPalindrome(const string &s) {
7+
string temp = s;
8+
reverse(temp.begin(),temp.end());
9+
return temp.compare(s) ? false : true ;
10+
}
11+
*/
12+
string longestPalindrome(string s) {
13+
int length = s.length();
14+
unordered_map<string,int> m;
15+
for(int l=length;l>=0;l--) {
16+
for(int i=0;i+l <= length; i++) {
17+
string temp(s,i,l);
18+
reverse(temp.begin(),temp.end());
19+
if (s.compare(i,l,temp) == 0){
20+
reverse(temp.begin(),temp.end());
21+
return temp;
22+
}
23+
}
24+
}
25+
26+
return "";
27+
}
28+
};
29+
30+
string stringToString(string input) {
31+
assert(input.length() >= 2);
32+
string result;
33+
for (int i = 1; i < input.length() -1; i++) {
34+
char currentChar = input[i];
35+
if (input[i] == '\\') {
36+
char nextChar = input[i+1];
37+
switch (nextChar) {
38+
case '\"': result.push_back('\"'); break;
39+
case '/' : result.push_back('/'); break;
40+
case '\\': result.push_back('\\'); break;
41+
case 'b' : result.push_back('\b'); break;
42+
case 'f' : result.push_back('\f'); break;
43+
case 'r' : result.push_back('\r'); break;
44+
case 'n' : result.push_back('\n'); break;
45+
case 't' : result.push_back('\t'); break;
46+
default: break;
47+
}
48+
i++;
49+
} else {
50+
result.push_back(currentChar);
51+
}
52+
}
53+
return result;
54+
}
55+
56+
int main() {
57+
string line;
58+
while (getline(cin, line)) {
59+
string s = stringToString(line);
60+
61+
string ret = Solution().longestPalindrome(s);
62+
63+
string out = (ret);
64+
cout << out << endl;
65+
}
66+
return 0;
67+
}

0 commit comments

Comments
 (0)