File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun isPalindrome (s : String ): Boolean {
3+ // ๋ฌธ์ฅ์ ๊ธธ์ด๋ ์ต๋ 20๋ง
4+ // ๋ฌธ์์ด์ด ๊ณต๋ฐฑ์ด๋ฉด return true
5+ // for-loop ํํด์ ์์ชฝ ๋์์๋ถํฐ ์์. ์์ชฝ index๊ฐ ์๋ก ๊ต์ฐจ๋๋ฉด return true
6+ // ๊ฐ๊ฐ char์ด alphanumeric์ด ์๋๋ฉด ํ์นธ ๋์ด๋.
7+ // ์๋ฌธ ๋๋ฌธ์๋ฉด ์๋ฌธ ์๋ฌธ์๋ก ์นํ.
8+ // - ์์ชฝ ๋์ char๋ฅผ ๋น๊ตํด์ ๊ฐ์ผ๋ฉด ์์ชฝ index๋ฅผ ์ด๋์ํด.
9+ // - ์์ชฝ ๋์ char๋ฅผ ๋น๊ตํด์ ๊ฐ์ง์์ผ๋ฉด return false
10+ // ์๊ฐ๋ณต์ก๋ : O(n/2)
11+ // ๊ณต๊ฐ๋ณต์ก๋ : O(n)
12+ if (s.isEmpty()) {
13+ return true
14+ }
15+
16+ var left = 0
17+ var right = s.length - 1
18+
19+ while (left <= right) { // ์๋ก ๊ต์ฐจํ๊ธฐ ์ ๊น์ง๋ง ๋น๊ตํ๋ฉด ๋จ (<= ๋์ < ์ฌ์ฉ ๊ฐ๋ฅ)
20+
21+ // 1. Char๊ฐ ์๋ฌธ์๋ ์ซ์๊ฐ ์๋์ง ํ์ธ
22+ if (! s[left].isLetterOrDigit()) {
23+ left + = 1
24+ continue
25+ }
26+
27+ if (! s[right].isLetterOrDigit()) {
28+ right - = 1
29+ continue
30+ }
31+
32+ // 2. ๋์๋ฌธ์ ํต์ผ ํ ๋น๊ต
33+ if (s[left].lowercaseChar() != s[right].lowercaseChar()) {
34+ return false
35+ } else {
36+ left + = 1
37+ right - = 1
38+ }
39+ }
40+
41+ return true
42+ }
43+ }
You canโt perform that action at this time.
0 commit comments