๋ฐ์ํ
    
    
    
  ์ธ๊ณ์ด ์ฌ์ 
๋ฌธ์  ์ค๋ช
PROGRAMMERS-962 ํ์ฑ์ ๋ถ์์ฐฉํ ์ฐ์ฃผ๋นํ์ฌ ๋จธ์ฑ์ด๋ ์ธ๊ณํ์ฑ์ ์ธ์ด๋ฅผ ๊ณต๋ถํ๋ ค๊ณ ํฉ๋๋ค. ์ํ๋ฒณ์ด ๋ด๊ธด ๋ฐฐ์ด spell๊ณผ ์ธ๊ณ์ด ์ฌ์  dic์ด ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง๋๋ค. spell์ ๋ด๊ธด ์ํ๋ฒณ์ ํ๋ฒ์ฉ๋ง ๋ชจ๋ ์ฌ์ฉํ ๋จ์ด๊ฐ dic์ ์กด์ฌํ๋ค๋ฉด 1, ์กด์ฌํ์ง ์๋๋ค๋ฉด 2๋ฅผ returnํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ ์ฌํญ
- spell๊ณผ dic์ ์์๋ ์ํ๋ฒณ ์๋ฌธ์๋ก๋ง ์ด๋ฃจ์ด์ ธ์์ต๋๋ค.
- 2 ≤ spell์ ํฌ๊ธฐ ≤ 10
- spell์ ์์์ ๊ธธ์ด๋ 1์ ๋๋ค.
- 1 ≤ dic์ ํฌ๊ธฐ ≤ 10
- 1 ≤ dic์ ์์์ ๊ธธ์ด ≤ 10
- spell์ ์์๋ฅผ ๋ชจ๋ ์ฌ์ฉํด ๋จ์ด๋ฅผ ๋ง๋ค์ด์ผ ํฉ๋๋ค.
- spell์ ์์๋ฅผ ๋ชจ๋ ์ฌ์ฉํด ๋ง๋ค ์ ์๋ ๋จ์ด๋ dic์ ๋ ๊ฐ ์ด์ ์กด์ฌํ์ง ์์ต๋๋ค.
- dic๊ณผ spell ๋ชจ๋ ์ค๋ณต๋ ์์๋ฅผ ๊ฐ์ง ์์ต๋๋ค.
์ ์ถ๋ ฅ ์
| spell | dic | result | 
| ["p", "o", "s"] | ["sod", "eocd", "qixm", "adio", "soo"] | 2 | 
| ["z", "d", "x"] | ["def", "dww", "dzx", "loveaw"] | 1 | 
| ["s", "o", "m", "d"] | ["moos", "dzx", "smm", "sunmmo", "som"] | 2 | 
์ ์ถ๋ ฅ ์ ์ค๋ช
์ ์ถ๋ ฅ ์ #1
- "p", "o", "s" ๋ฅผ ์กฐํฉํด ๋ง๋ค ์ ์๋ ๋จ์ด๊ฐ dic์ ์กด์ฌํ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ 2๋ฅผ returnํฉ๋๋ค.
์ ์ถ๋ ฅ ์ #2
- "z", "d", "x" ๋ฅผ ์กฐํฉํด ๋ง๋ค ์ ์๋ ๋จ์ด "dzx"๊ฐ dic์ ์กด์ฌํฉ๋๋ค. ๋ฐ๋ผ์ 1์ returnํฉ๋๋ค.
์ ์ถ๋ ฅ ์ #3
- "s", "o", "m", "d" ๋ฅผ ์กฐํฉํด ๋ง๋ค ์ ์๋ ๋จ์ด๊ฐ dic์ ์กด์ฌํ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ 2์ returnํฉ๋๋ค.
์ ์์ฌํญ
- ์ ์ถ๋ ฅ ์ #3 ์์ "moos", "smm", "som"๋ "s", "o", "m", "d" ๋ฅผ ์กฐํฉํด ๋ง๋ค ์ ์์ง๋ง spell์ ์์๋ฅผ ๋ชจ๋ ์ฌ์ฉํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ ๋ต์ด ์๋๋๋ค.
์ ์ถ
import Foundation
func solution(_ spell:[String], _ dic:[String]) -> Int {
    for d in dic{
        var flag = false
        
        for s in spell{
            if d.contains(s){
                flag = true
            }else{
                flag = false
                break
            }
        }
        
        if flag && d.count == spell.count{
            return 1
        }
    }
    return 2
}
๋ค๋ฅธ ํ์ด
import Foundation
func solution(_ spell: [String], _ dic: [String]) -> Int { 
  return dic.map { String($0.sorted()) }.contains(spell.sorted().joined()) ? 1 : 2 
}๋ฐ์ํ
    
    
    
  'โจ๏ธ Language > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [ํ๋ก๊ทธ๋๋จธ์ค LV.0] ํํ (0) | 2023.01.03 | 
|---|---|
| [ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์ ์ฃผ์ ์ซ์ 3 (0) | 2023.01.03 | 
| [ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์ผ๊ฐํ์ ์์ฑ์กฐ๊ฑด (2) (0) | 2023.01.02 | 
| [ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์์ ์ง๋ (0) | 2023.01.02 | 
| [ํ๋ก๊ทธ๋๋จธ์ค LV.0] ์จ์ด์๋ ์ซ์์ ๋ง์  (2) (0) | 2023.01.02 |