Skip to content

Commit d9bfde0

Browse files
authored
Create 214. Shortest Palindrome (#590)
2 parents ab70488 + cf460b4 commit d9bfde0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

214. Shortest Palindrome

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string shortestPalindrome(string s) {
4+
ios_base::sync_with_stdio(false);
5+
cin.tie(NULL);
6+
cout.tie(NULL);
7+
// Note:
8+
9+
10+
11+
string originalString = s;
12+
reverse(s.begin(), s.end());
13+
14+
int sz = s.size();
15+
16+
for(int i = 0 ; i <= sz - 1 ; i++){
17+
if(!memcmp(originalString.c_str(), s.c_str() + i, sz - i)){
18+
return s.substr(0, i) + originalString;
19+
}
20+
}
21+
22+
return s + originalString;
23+
24+
}
25+
};

0 commit comments

Comments
 (0)