주녘공부일지

[프로그래머스 C#] Lv.2 영어 끝말잇기 본문

Programmers - C#/CodingTest Lv.2

[프로그래머스 C#] Lv.2 영어 끝말잇기

주녘 2023. 12. 4. 15:52
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/12981

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1. 정답코드 및 핵심 아이디어, 유의사항

n명의 사람이 끝말잇기를 했을 때, 틀린 사람에 대해 [ 몇번 째 사람, 몇번 째 순서 ] 로 반환하면 되는 문제

 

끝말잇기의 틀리는 조건

- 앞 순서의 마지막 문자와 현재 순서의 첫 문자가 같아야 함

- 앞에 나온 문자열이 아니여야 함 // List.Contains로 체크

 

주석참조

    using System;
    using System.Collections.Generic;

    class Solution
    {
        public int[] solution(int n, string[] words)
        {
            var list = new List<string>();

            // 초기 값 세팅
            list.Add(words[0]);

            for (int i = 1; i < words.Length; i++)
            {
                // 틀리는 조건
                if (list.Contains(words[i]) || words[i - 1][words[i - 1].Length - 1] != words[i][0])
                    return new int[2] { i % n + 1, i / n + 1 };

                list.Add(words[i]);
            }

            return new int[2] { 0, 0 };
        }
    }
728x90