728x90
๋ฐ์ํ
Problem
1254๋ฒ: ํฐ๋ฆฐ๋๋กฌ ๋ง๋ค๊ธฐ
๋ํธ์ ๊ท์์ด๋ 212ํธ์์ ๋ฌธ์์ด์ ๋ํด ๊ณต๋ถํ๊ณ ์๋ค. ๊ท์์ด๋ ํฐ๋ฆฐ๋๋กฌ์ ์์ฒญ๋๊ฒ ์ข์ํ๋ค. ํฐ๋ฆฐ๋๋กฌ์ด๋ ์์์๋ถํฐ ์ฝ์ผ๋ ๋ค์์๋ถํฐ ์ฝ์ผ๋ ๊ฐ๊ฒ ์ฝํ๋ ๋ฌธ์์ด์ ๋งํ๋ค. ๋ํธ๋
www.acmicpc.net
Solution
1. ์ฃผ์ด์ง ๋ฌธ์๋ฅผ ๋ค์ง์ด์ ๋ค์ ๋ถ์ธ๋ค.
var str = readLine()!
var newStr = (str + str.reversed()).map{String($0)}
2. ๋ค์ ๋ถ์ธ ๊ณณ์ ์์์ ์ผ๋ก ์ฃผ์ด์ง ๋ฌธ์๊ฐ ๋ ๋๊น์ง ์ญ์ ํ๋ค.
while newStr.count >= str.count {
if isPalindrome(newStr) {
min = newStr.count
}
if newStr.count == str.count {
break
}
newStr.remove(at: start)
}
3. ํฐ๋ฆฐ๋๋กฌ์ ํ์ธํ๊ณ ์ต์ ๊ธธ์ด๋ฅผ ๊ฐฑ์ ํ๋ค.
func isPalindrome(_ str:[String]) -> Bool {
let middle = str.count%2 == 0 ? str.count/2-1 : str.count/2
let head = str[0...middle].joined()
let tail = str[str.count/2..<str.count].reversed().joined()
return head == tail
}
Source Code
728x90
๋ฐ์ํ
'๐ Problem Solution' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Baekjoon ๋์ด์ ์ ๋ ฌ 10814๋ฒ (0) | 2022.01.30 |
---|---|
[Swift] Baekjoon 12919๋ฒ A์ B 2 (0) | 2022.01.07 |
[Swift] Baekjoon ์ต๋๊ฐ 2562๋ฒ (0) | 2022.01.06 |
[Swift] Baekjoon ํฐ ์ A + B 10757๋ฒ (0) | 2022.01.06 |
[Swift] 2020 KAKAO BLIND RECRUITMENT ์ธ๋ฒฝ ์ ๊ฒ (0) | 2021.09.08 |
๋๊ธ