본문 바로가기
Algorithm

SwiftUI: 신규 아이디 추천

by songmoro 2023. 12. 17.
728x90

주어진 id를 7단계의 검사 후 결과를 반환하는 문제입니다.

정규 표현식을 사용해서 검사합니다.

 

func solution(_ new_id:String) -> String {
    var new_id = new_id
    
    new_id = new_id.lowercased()
    
    new_id = new_id.replacingOccurrences(of: "[^a-z0-9-_.]", with: "", options: .regularExpression)
    
    new_id = new_id.replacingOccurrences(of: "(\\\\.)\\\\1+", with: ".", options: .regularExpression)
    
    new_id = new_id.replacingOccurrences(of: "^\\\\.", with: "", options: .regularExpression).replacingOccurrences(of: "\\\\.$", with: "", options: .regularExpression)
    
    new_id = new_id.isEmpty ? "a" : new_id
    
    new_id = String(new_id.prefix(15)).replacingOccurrences(of: "\\\\.$", with: "", options: .regularExpression)
    
    while(new_id.count <= 2) {
        new_id.append(new_id.last!)
    }
    
    return new_id
}
728x90

'Algorithm' 카테고리의 다른 글

SwiftUI: 햄버거 만들기  (0) 2023.12.17
SwiftUI: 신고 결과 받기  (0) 2023.12.17
SwiftUI: 둘만의 암호  (1) 2023.12.17
SwiftUI: 카드 뭉치  (0) 2023.12.17
SwiftUI: 대충 만든 자판  (0) 2023.12.17