๋ฐ์ํ
๋ฌธ์
https://leetcode.com/problems/valid-palindrome/description/
Palindrome ๋ฌธ๊ตฌ๋ ์ฃผ์ด์ง ๋ฌธ์์ด์์ ๋ชจ๋ ๋๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ฐ๊พผ ํ, ์์ซ์๊ฐ ์๋ ๋ฌธ์๋ค์ ์ง์ ์ ๋, ์๋ค๋ก ๋๊ฐ์ด ์ฝํ๋ค. Alphanumeric(์์ซ์)๋ ๋ฌธ์์ ์ซ์๋ฅผ ํฌํจํ๋ค.
ํ์ด
1. ์ฝํ๋ฆฐ ํ์ฅํจ์๋ฅผ ๋ง๊ป ํ์ฉํ ํ์ด
fun isPalindrome(s: String): Boolean = s.lowercase().filter {
it.isLetterOrDigit()
}.let {
it == it.reversed()
}
๋จ ๋์ค๋ก ๋ฌธ์ ๋ฅผ ํ ์ ์์๋ค.
๋ค๋ง leetcode ์ฝํ๋ฆฐ ๋ฒ์ ์ด ๋ฎ์์์ธ์ง Deprecated๋ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ์ผ ํจ! (toLowerCase)
์๊ฐ ๋ณต์ก๋: O(n), ๊ณต๊ฐ ๋ณต์ก๋: O(n)
2. ํฌํฌ์ธํฐ๋ฅผ ํ์ฉํ ํ์ด
fun isPalindromeTwoPointer(s: String): Boolean {
var left: Int = 0
var right: Int = s.length - 1
while(left < right) {
while(!s[left].isLetterOrDigit() && left < right) left ++
while(!s[right].isLetterOrDigit() && right > left) right --
if(s[left].lowercase() != s[right].lowercase()) return false
left ++
right --
}
return true
}
์๊ฐ ๋ณต์ก๋: O(n), ๊ณต๊ฐ ๋ณต์ก๋: O(1)
https://github.com/jeongum/algorithm/blob/master/LT_125_ValidPalindrome.kt
๋ฐ์ํ
'๐ ์๊ณ ๋ฆฌ์ฆ > ๊ธฐ๋ณธ๋ฌธ๋ฒ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 412. Fizz Buzz (ํด์, Kotlin ํ์ด) (0) | 2023.06.12 |
---|---|
[๋ฐฑ์ค] 2941๋ฒ_ํฌ๋ก์ํฐ์ ์ํ๋ฒณ (0) | 2021.07.24 |
[๋ฐฑ์ค] 1157๋ฒ_๋จ์ด๊ณต๋ถ (0) | 2021.07.24 |
[๋ฐฑ์ค] 1065๋ฒ_ํ์ (0) | 2021.07.24 |
[๋ฐฑ์ค] 4673๋ฒ_์ ํ๋๋ฒ (0) | 2021.07.24 |