๊ธ€ ์ž‘์„ฑ์ž: ๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด

์ œ๋ชฉ

๋ฌธ์ œ ์„ค๋ช…

1๋ถ€ํ„ฐ n๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด์žˆ๋Š” n๋ช…์˜ ์‚ฌ๋žŒ์ด ์˜์–ด ๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด ๋๋ง์ž‡๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

  1. 1๋ฒˆ๋ถ€ํ„ฐ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์‚ฌ๋žŒ์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ๋‹จ์–ด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
  2. ๋งˆ์ง€๋ง‰ ์‚ฌ๋žŒ์ด ๋‹จ์–ด๋ฅผ ๋งํ•œ ๋‹ค์Œ์—๋Š” ๋‹ค์‹œ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  3. ์•ž์‚ฌ๋žŒ์ด ๋งํ•œ ๋‹จ์–ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ๋งํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  4. ์ด์ „์— ๋“ฑ์žฅํ–ˆ๋˜ ๋‹จ์–ด๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  5. ํ•œ ๊ธ€์ž์ธ ๋‹จ์–ด๋Š” ์ธ์ •๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ 3๋ช…์ด ๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜๋Š” ์ƒํ™ฉ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

tank → kick → know → wheel → land → dream → mother → robot → tank

์œ„ ๋๋ง์ž‡๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

  • 1๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋ก€์— tank๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • 2๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋ก€์— kick์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • 3๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋ก€์— know๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • 1๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ๋‘ ๋ฒˆ์งธ ์ฐจ๋ก€์— wheel์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • (๊ณ„์† ์ง„ํ–‰)

๋๋ง์ž‡๊ธฐ๋ฅผ ๊ณ„์† ์ง„ํ–‰ํ•ด ๋‚˜๊ฐ€๋‹ค ๋ณด๋ฉด, 3๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์„ธ ๋ฒˆ์งธ ์ฐจ๋ก€์— ๋งํ•œ tank ๋ผ๋Š” ๋‹จ์–ด๋Š” ์ด์ „์— ๋“ฑ์žฅํ–ˆ๋˜ ๋‹จ์–ด์ด๋ฏ€๋กœ ํƒˆ๋ฝํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์‚ฌ๋žŒ์˜ ์ˆ˜ n๊ณผ ์‚ฌ๋žŒ๋“ค์ด ์ˆœ์„œ๋Œ€๋กœ ๋งํ•œ ๋‹จ์–ด words ๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ€์žฅ ๋จผ์ € ํƒˆ๋ฝํ•˜๋Š” ์‚ฌ๋žŒ์˜ ๋ฒˆํ˜ธ์™€ ๊ทธ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ๋ช‡ ๋ฒˆ์งธ ์ฐจ๋ก€์— ํƒˆ๋ฝํ•˜๋Š”์ง€๋ฅผ ๊ตฌํ•ด์„œ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

ํ’€์ด

์ค‘๋ณต๋˜๋Š” ๋‹จ์–ด์˜ ์œ ๋ฌด๋Š” Set์„ ํ†ตํ•ด์„œ, ๋งˆ์ง€๋ง‰ ๋ ๋ฌธ์ž์™€ ํ˜„์žฌ ์•ž ๋ฌธ์ž๊ฐ€ ๊ฐ™์€์ง€ ๋น„๊ตํ•ด์„œ ๋๋ง์ž‡๊ธฐ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ง„ํ–‰๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ž˜๋ชป๋œ ๋‹จ์–ด๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด ํ˜„์žฌ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ๋ˆ„๊ตฌ(index % n + 1)์˜ ๋ช‡๋ฒˆ์งธ(index / n + 1) ๋‹จ์–ด์ธ์ง€๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๊นŒ์ง€ ๋‹ค ํ™•์ธํ–ˆ์„ ๋•Œ๋„ ๋๋ง์ž‡๊ธฐ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ง„ํ–‰๋˜์—ˆ๋‹ค๋ฉด [0, 0]์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ

import Foundation

func solution(_ n:Int, _ words:[String]) -> [Int] {
    var wordSet = Set<String>()
    for (index, word) in words.enumerated() {
        if !wordSet.isEmpty && (wordSet.contains(word) || words[index-1].last != word.first){
            return [(index % n) + 1, index / n + 1]
        }
        wordSet.insert(word)
    }
    
    return [0, 0]
}