Skip to content

Commit 6dbea54

Browse files
author
แ„‹แ…ตแ„‹แ…งแ†ซแ„‰แ…ฎ
committed
Valid Palindrome
1 parent 842f1fc commit 6dbea54

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode_study
2+
3+
/**
4+
* ๋ฌธ์ž์—ด์˜ ๋Œ€์นญ ํŒ๋‹จ
5+
* ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(n)
6+
* -> ์ฃผ์–ด์ง„(n) ํฌ๊ธฐ์˜ ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๋น„๊ตํ•จ
7+
*/
8+
fun isPalindrome(s: String): Boolean {
9+
10+
// ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ๋ชจ๋“  non-alphanumeric characters๋ฅผ ํ•„ํ„ฐํ•œ ๋ฌธ์ž ๋ฐฐ์—ด ํ• ๋‹น
11+
val splitGivenString = s.toCharArray()
12+
.filter { it.isLetter() || it.isDigit() }
13+
.map { it.lowercaseChar() }
14+
.toCharArray()
15+
16+
// ํ•„ํ„ฐ๋œ ๋ฌธ์ž์—ด์ด ๋น„์–ด์žˆ๋‹ค๋ฉด true ๋ฐ˜ํ™˜
17+
if (splitGivenString.isEmpty()) { return true }
18+
19+
// ๋Œ€์นญ์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ ์‹œ์ž‘, ๋ ๋ณ€์ˆ˜
20+
var start = 0
21+
var end = splitGivenString.size - 1
22+
23+
// ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋ฉฐ ๊ฐ™๋‹ค๋ฉด ์‹œ์ž‘ +1, ๋ -1 ์„ ์ง„ํ–‰ํ•ด ๋Œ€์นญ ํŒ๋‹จ
24+
while (start <= end) {
25+
if (splitGivenString[start] == splitGivenString[end]) {
26+
start += 1
27+
end -= 1
28+
continue
29+
}
30+
return false
31+
}
32+
return true
33+
}

0 commit comments

Comments
ย (0)