Skip to content

Commit 138a294

Browse files
committed
Solution: Valid Palindrome
1 parent 05c0932 commit 138a294

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

valid-palindrome/flynn.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 풀이
3+
* - 주어진 string `s`의 양 끝에서부터 차례대로 비교해가며 palindrome 여부를 판단합니다
4+
*
5+
* Big-O
6+
* - N: 주어진 string `s`의 길이
7+
*
8+
* - Time Complexity: O(N)
9+
* - `s`가 palindrome인 경우, `s` 전체를 탐색하므로 O(N)의 시간복잡도를 가집니다
10+
*
11+
* - Space Complexity: O(1)
12+
* - 입력값과 무관하게 일정한 저장공간을 사용합니다
13+
*/
14+
15+
class Solution {
16+
public:
17+
bool isPalindrome(string s) {
18+
string::iterator lo = s.begin();
19+
string::iterator hi = s.end();
20+
21+
while (lo <= hi) {
22+
if (isalnum(*lo) && isalnum(*hi)) {
23+
if (tolower(*lo) != tolower(*hi)) return false;
24+
else {
25+
++lo;
26+
--hi;
27+
continue;
28+
}
29+
}
30+
if (!isalnum(*lo)) ++lo;
31+
if (!isalnum(*hi)) --hi;
32+
}
33+
34+
return true;
35+
}
36+
};

0 commit comments

Comments
 (0)