๐ฌย ๋ฌธ์
https://app.codility.com/programmers/trainings/1/longest_password/
๐ฌย Idea
- Swift4์์ filter ๋ด์ Character์์ isLetter, isNumber ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๋ ๊ฒ ๊ฐ๋ค.. ๊ทธ๋์ ์ ๊ทํํ์์ ์ฌ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํ์ดํ๋ค.
- ๋ฌธ์๊ฐ ์ง์๊ฐ์ผ ๊ฒ ์กฐ๊ฑด์ ๊ฒ์ฌํด์ฃผ๊ธฐ ์ํด ์ซ์์ธ ๋ฌธ์๋ค์ โโ์ผ๋ก ์นํํด์ฃผ์๋ค. ์นํ ํ์๋ ๋ฌธ์๋ง ๋จ์ผ๋ฏ๋ก ํด๋น ๊ฐ์๋ฅผ ์ธ์ด ํ๋ณํ๋ค.
๐ฌย ํ์ด
publicfunc solution(_ S :inoutString)->Int{lets=S.components(separatedBy:.whitespaces).filter({ $0.count %2==1})varlongestSCount=-1foriin s {ifcheckIsValidString(i){if longestSCount < i.count {
longestSCount = i.count
}}}return longestSCount
}publicfunc checkIsValidString(_ S:String)->Bool{
// 1. ์ซ์์ ์๋ฌธ์๋ง ํฌํจํ ๊ฒ
letregexPattern="^[0-9a-zA-Z]*$"guardlet _ =S.range(of: regexPattern, options:.regularExpression)else{returnfalse}
// 2. ๋ฌธ์๊ฐ ์ง์๊ฐ์ผ ๊ฒ
ifS.replacingOccurrences(of:"[0-9]", with:"", options:.regularExpression).count %2!=0{returnfalse}returntrue}ํ๊ฐํ : https://app.codility.com/demo/results/trainingH74JG3-CYY/
๐ฌย ๋ฌธ์ https://app.codility.com/programmers/trainings/1/longest_password/
๐ฌย Idea๐ฌย ํ์ดํ๊ฐํ: https://app.codility.com/demo/results/trainingH74JG3-CYY/