[프로그래머스] 신규 아이디 추천 Swift

AekT·2021년 11월 12일
0

프로그래머스

목록 보기
6/6
post-thumbnail

프로그래머스 - 신규 아이디 추천

문제 : https://programmers.co.kr/learn/courses/30/lessons/72410

Code :

import Foundation
func solution(_ new_id:String) -> String {
    var s = new_id.lowercased().filter{  $0.isLetter || $0.isNumber || $0 == "-" || $0 == "_" || $0 == "." }.map{String($0)}.joined()
    while s.contains(".."){s = s.replacingOccurrences(of: "..", with: ".")}
    if let a = s.first, a == "."{s.removeFirst()};
    if let a = s.last, a == "."{s.removeLast()}
    if s.isEmpty{ s = "a" }
    if s.count>=16 {
        s = String(s.prefix(15))
        if s.last! == "."{
            s.removeLast()
        }
    }
    while s.count < 3{s += String(s.last!)}
    
    
    return s
}
profile
으악

0개의 댓글