코딩테스트/코딩테스트 Python

[CodeSignal] firstNotRepeatingCharacter (Python)

sseozytank 2022. 9. 19.

https://app.codesignal.com/interview-practice/task/uX5iLwhc6L5ckSyNC/description

 

firstNotRepeatingCharacter | CodeSignal

Press space bar to start a drag. When dragging you can use the arrow keys to move the item around and escape to cancel. Some screen readers may require you to be in focus mode or to use your pass through key

app.codesignal.com

✔️문제 요약 

1.주어진 문자열에서, 첫번째로 등장한 단하나의 문자를 출력한다. 

2.주어진 문자열에 혼자 등장한 문자가 없으면, '_'을 출력 

 

✔️풀이 방법 

1.array 이지만, 이 문제를 보자마자 Counter 함수를 떠올렸다. 파이썬에서 가끔 전처리를 하며 해당함수를 쓰는데 

익숙해지면 좋을 것 같아서 나는 Counter 함수를 통해 풀었다. 

from collections import Counter

def solution(s):
    dic=Counter(s).most_common()
    for key, value in dic:
        if value == 1:
            return key
    return '_'

 

✔️고찰

-리스트로 풀게된다면, index를 이용해서 풀었을 것 같다. 

 

댓글