-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Labels
Description
๐ฌย ๋ฌธ์
https://app.codility.com/programmers/trainings/1/longest_password/
๐ฌย Idea
- Swift4์์ filter ๋ด์ Character์์ isLetter, isNumber ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๋ ๊ฒ ๊ฐ๋ค.. ๊ทธ๋์ ์ ๊ทํํ์์ ์ฌ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํ์ดํ๋ค.
-
- ๋ฌธ์๊ฐ ์ง์๊ฐ์ผ ๊ฒ ์กฐ๊ฑด์ ๊ฒ์ฌํด์ฃผ๊ธฐ ์ํด ์ซ์์ธ ๋ฌธ์๋ค์ โโ์ผ๋ก ์นํํด์ฃผ์๋ค. ์นํ ํ์๋ ๋ฌธ์๋ง ๋จ์ผ๋ฏ๋ก ํด๋น ๊ฐ์๋ฅผ ์ธ์ด ํ๋ณํ๋ค.
๐ฌย ํ์ด
public func solution(_ S : inout String) -> Int {
let s = S.components(separatedBy: .whitespaces).filter({ $0.count % 2 == 1 })
var longestSCount = -1
for i in s {
if checkIsValidString(i) {
if longestSCount < i.count {
longestSCount = i.count
}
}
}
return longestSCount
}
public func checkIsValidString(_ S: String) -> Bool {
// 1. ์ซ์์ ์๋ฌธ์๋ง ํฌํจํ ๊ฒ
let regexPattern = "^[0-9a-zA-Z]*$"
guard let _ = S.range(of: regexPattern, options: .regularExpression) else { return false }
// 2. ๋ฌธ์๊ฐ ์ง์๊ฐ์ผ ๊ฒ
if S.replacingOccurrences(of: "[0-9]", with: "", options: .regularExpression).count % 2 != 0 { return false }
return true
}
ํ๊ฐํ
: https://app.codility.com/demo/results/trainingH74JG3-CYY/