2351. First Letter to Appear Twice
LeetCode
https://leetcode.com/problems/first-letter-to-appear-twice/
문제
- 문자열 s가 주어졌습니다.
- s는 영어 소문자로 이루어져 있습니다.
- s에서 처음으로 두 번 등장하는 문자를 반환하는 코드를 작성하세요.
예제
- Input: s = "abccbaacz"
- Output: "c"
풀이 과정
- 빈 리스트 visited를 생성합니다.
- 인덱스 i를 사용하여 주어진 문자열 s의 모든 문자를 순회하며 다음 조건을 수행합니다.
- 현재 문자(s[i])가 visited에 있다면 해당 문자를 반환합니다.
- 그렇지 않으면 현재 문자를 visited에 추가합니다.
코드(Python)
class Solution:
def repeatedCharacter(self, s: str) -> str:
visited = []
for i in range(len(s)):
if s[i] in visited:
return s[i]
visited.append(s[i])
참고
- set( ) : 중복을 허용하지 않는 집합 생성
'알고리즘 스터디 > Python' 카테고리의 다른 글
| [LeetCode] 2124. Check if All A's Appears Before All B's (0) | 2024.09.20 |
|---|---|
| [LeetCode] 2357. Make Array Zero by Subtracting Equal Amounts (0) | 2024.09.17 |
| [LeetCode] 1385. Find the Distance Value Between Two Arrays (0) | 2024.09.12 |
| [LeetCode] 1331. Rank Transform of an Array (0) | 2024.09.12 |
| [LeetCode] 3178. Find the Child Who Has the Ball After K Seconds (0) | 2024.09.10 |