Skip to content

Commit 2488680

Browse files
committed
#220 valid-palindrome solution
1 parent 3dc6c7d commit 2488680

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

valid-palindrome/sungjinwi.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
풀이 :
3+
alnum인 문자들만 추출해서 소문자로 만들어 alnu_s를 만듬
4+
양 끝이 같은 문자일 동안 left++, right-- 실시
5+
같지 않으면 false, 양 끝이 수렴되서 서로 만난다면 true
6+
7+
문자 길이 N
8+
9+
TC : O(N)
10+
11+
SC : O(N)
12+
*/
13+
14+
#include <string>
15+
using namespace std;
16+
17+
class Solution {
18+
public:
19+
bool isPalindrome(string s) {
20+
string alnu_s = "";
21+
22+
for (char c : s)
23+
{
24+
if (isalnum(c))
25+
alnu_s += tolower(c);
26+
}
27+
int left = 0;
28+
int right = alnu_s.size() - 1;
29+
while (left < right)
30+
{
31+
if (alnu_s[left] != alnu_s[right])
32+
return false;
33+
left++;
34+
right--;
35+
}
36+
return true;
37+
}
38+
};

0 commit comments

Comments
 (0)