๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ Programmers(Lv.2)

by seolhee2750 2021. 7. 8.
๋ฌธ์ œ ์„ค๋ช…

JadenCase๋ž€ ๋ชจ๋“  ๋‹จ์–ด์˜ ์ฒซ ๋ฌธ์ž๊ฐ€ ๋Œ€๋ฌธ์ž์ด๊ณ , ๊ทธ ์™ธ์˜ ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์ž์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, s๋ฅผ JadenCase๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์กฐ๊ฑด
  • s๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • s๋Š” ์•ŒํŒŒ๋ฒณ๊ณผ ๊ณต๋ฐฑ๋ฌธ์ž(" ")๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฒซ ๋ฌธ์ž๊ฐ€ ์˜๋ฌธ์ด ์•„๋‹๋•Œ์—๋Š” ์ด์–ด์ง€๋Š” ์˜๋ฌธ์€ ์†Œ๋ฌธ์ž๋กœ ์”๋‹ˆ๋‹ค. ( ์ฒซ๋ฒˆ์งธ ์ž…์ถœ๋ ฅ ์˜ˆ ์ฐธ๊ณ  )

 

์ž…์ถœ๋ ฅ ์˜ˆ
s return
"3people unFollowed me" "3people Unfollowed Me"
"for the last week" "For The Last Week"

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
func solution(_ s:String) -> String {
    let upperString = Array("abcdefghijklmnopqrstuvwxyz".uppercased())
    let lowerString = Array("abcdefghijklmnopqrstuvwxyz")
    let arr = s.components(separatedBy: " ") // ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆˆ ๋ฐฐ์—ด
    var result = [String]()

    for i in 0..<arr.count {
        for j in arr[i] {
            // ์•ŒํŒŒ๋ฒณ์ผ ๋•Œ
            if upperString.contains(j) || lowerString.contains(j) {
                result.append(arr[i].capitalized) // ์ฒซ ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ
                break // ๋ฐ”๋กœ ํƒˆ์ถœ
            }
            
            // ์•ŒํŒŒ๋ฒณ ์•„๋‹ ๋•Œ
            else {
                result.append(arr[i].lowercased()) // ์ „๋ถ€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ
                break // ๋ฐ”๋กœ ํƒˆ์ถœ
            }
        }
        
        // ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๋‹จ์–ด ์ „๊นŒ์ง€๋Š” ํ•œ ๋‹จ์–ด๋งˆ๋‹ค ๋ฐฐ์—ด์— ๊ณต๋ฐฑ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ
        if i < arr.count-1 {
            result.append(" ")
        }
    }
    
    return result.joined()
}
  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
  • ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž๋ฅผ ํŒ๋ณ„ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž ๋ฐฐ์—ด์„ ๊ฐ๊ฐ ์ƒ์„ฑํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋ฐฐ์—ด ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ, ๋ฌธ์ž์—ด ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๊ฐ€ ์•ŒํŒŒ๋ฒณ์ผ ๊ฒฝ์šฐ capitalized๋ฅผ ์‚ฌ์šฉํ•ด ์ฒซ ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€๋Š” ์†Œ๋ฌธ์ž๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๊ฐ€ ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹ ๊ฒฝ์šฐ ๋ชจ๋“  ๊ธ€์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์˜ ํ•œ ์š”์†Œ ๋งˆ๋‹ค JadenCase ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”์ค€ ํ›„์—๋Š” result ๋ฐฐ์—ด์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰์—” joined๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๋‹ค ํ’€๊ณ  ๋ณด๋‹ˆ๊นŒ ๊ณ ์ณ์•ผ ํ•  ์ ์ด ๋งŽ์•„๋ณด์ธ๋‹ค.ใ…œ
  • ์šฐ์„ , ๊ตณ์ด ์ž…๋ ฅ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค ํ•„์š” ์—†์ด, ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด๋“ค์„ ๊ฒ€์‚ฌํ•ด์คฌ์œผ๋ฉด ํ›จ์”ฌ ํšจ์œจ์ ์ผ ๊ฒƒ ๊ฐ™๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋ฐ˜ํ™˜ ๊ฐ’๋„ result ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ๋งŒ๋“ค๊ณ , joined๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ๊ตณ์ด ๊ทธ๋Ÿด ํ•„์š” ์—†์ด ๋ฐ”๋กœ ๋ฌธ์ž์—ด๋กœ appendํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค๋ฉด ๋” ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋˜์—ˆ์„ ๋“ฏ ํ•˜๋‹ค.

 


 

๐Ÿ– [ ์ถ”๊ฐ€ ] 1์ฃผ์ผ ํ›„ ๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ

func solution(_ s:String) -> String {
    var str = s.components(separatedBy: " ")
    var result = [String]()
    
    for i in 0..<str.count {
        var temp = 0
        for j in str[i] {
            if temp == 0 {
                result.append(String(j.uppercased()))
            }
            else if temp > 0 {
                result.append(String(j.lowercased()))
            }
            temp += 1
        }
        if i < str.count-1 {
            result.append(" ")
        }
    }
    
    return result.joined()
}
  • ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์„œ ๋ฐฐ์—ด ์ƒ์„ฑํ•ด์ฃผ์—ˆ๋‹ค.
  • temp ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž์ผ ๊ฒฝ์šฐ ๋Œ€๋ฌธ์ž๋กœ, ๋‚˜๋จธ์ง€๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋‹จ์–ด ์‚ฌ์ด์—๋Š” ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ž‘์„ฑํ•จ์œผ๋กœ์จ ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋ณด๋‹ค ๊ฐ„๊ฒฐํžˆ ์ž˜ ํ‘ผ ๊ฒƒ ๊ฐ™๋‹ค.

 


 

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/12951

 

๋Œ“๊ธ€