728x90
난이도 : medium
a, b, c로만 구성되어있고 'aa'나 'bb'처럼 동일한 알파벳이 반복되지 않는, 길이가 n인 happy string의 k번째 갯수를 return하는 문제
class Solution:
def getHappyString(self, n: int, k: int) -> str:
let = ['a', 'b', 'c']
for _ in range(1, n):
new = []
for prev in let:
for char in 'abc':
if prev[-1] != char:
new.append(prev + char)
let = new
return let[k-1] if len(let) >= k else ""
- let으로 a, b, c의 요소를 input
- let 내의 글자들을 prev로 받고, 마지막 요소가 char와 다르면 리스트 new 뒤에 append
- 리스트 let을 리스트 new로 바꾸기
- let의 요소 갯수가 k보다 크거나 동일하면 k-1번째 원소를 돌려주고, 아니면 빈 문자열을 return
Runtime : 27ms (50.11%)
Memory : 18.06MB (42.89%)
728x90
'코딩 공부 > Leetcode' 카테고리의 다른 글
[Python / Leetcode] 1980. Find Unique Binary String (0) | 2025.02.20 |
---|---|
[Leetcode / Python] 2375. Construct Smallest Number From DI String (0) | 2025.02.18 |
[Leetcode / Python] 3066. Minimum Operations to Exceed Threshold Value II (0) | 2025.02.13 |
[Leetcode / Python] 2364. Count Number of Bad Pairs (0) | 2025.02.13 |
[Leetcode / Python] 3174. Clear Digits (0) | 2025.02.12 |