Python 기본 사항쉬움

주어진 숫자 시퀀스의 가능한 디코딩 수 계산

'주어진 숫자 시퀀스의 가능한 디코딩 계산' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

숫자 문자열을 받아 이를 디코딩할 수 있는 가능한 방법의 수를 반환하는 함수 count_decodings(digits)를 작성하세요. 여기서 'A' = 1, 'B' = 2, ..., 'Z' = 26입니다.

예를 들어 '12'는 'AB'(1, 2) 또는 'L'(12)로 디코딩되어 두 가지 방법을 제공할 수 있습니다.

문자열의 잘못된 위치에 '0'이 포함되어 있는 경우(예: 선행 '0' 또는 '30') 해당 경로는 유효하지 않으며 계산되지 않습니다.

제약
  • 1 <= len(digits) <= 20
  • digits contains only characters '0' through '9'

Example 1
Input
count_decodings('12')
Output
2
Explanation

'12' can be decoded as 'AB' (1,2) or 'L' (12). So 2 ways.

Example 2
Input
count_decodings('226')
Output
3
Explanation

'226' can be decoded as 'BBF' (2,2,6), 'BZ' (2,26), or 'VF' (22,6). So 3 ways.

Example 3
Input
count_decodings('06')
Output
0
Explanation

'06' cannot be decoded because '0' has no letter mapping and '06' is not a valid code.

Need a Hint?
세트나 힙과 같은 Numbers 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.