Skip to content

[Algorithm] 영어 끝말잇기 #108

Description

@hwangJi-dev

💬 문제

영어 끝말잇기


💬 Idea

  • 중복일 경우를 판별해주기 위해 key값을 사용할 수 있는 Dictionary type의 dict를 만들어주어 끝말잇기가 된 단어들을 저장해준다.
  • 탈락조건: 한 글자 / 중복 단어 / 끝 글자와 이어지지 않는 경우
    • 해당 탈락 조건에 해당될 경우 ans 배열에 번호, 순서를 저장해준 뒤 반복문을 탈출해준다.
  • 문제에서 주어진 조건을 잘 읽어 테스트케이스를 잘 파악하자!!! (시간 낭비 줄이기)

💬 풀이

func solution(n:Int, words:[String])->[Int]{vardict:[String:Int]=[:]varans=[0,0]for(index, word)in words.enumerated(){
// 탈락 - 한 글자, 중복, 끝 말과 이어지지 않는 경우
if index !=0 && (dict[word]!=nil || word.count ==1 || words[index -1].last != word.first){
ans =[(index % n)+1,(index / n)+1]break}dict[word]= index
}return ans
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions