Total Decoding Messages: A DP Solution

This title was summarized by AI from the post below.

✅ Day 128 – GeeksforGeeks 160 Days of DSA Challenge Hi everyone, Today’s challenge was Total Decoding Messages — a classic Dynamic Programming problem based on string decoding and combinatorial counting. 🧩 Problem: A numeric string digits represents an encoded message where 'A' -> 1, 'B' -> 2, … 'Z' -> 26. The task is to determine how many possible ways the string can be decoded into letters. For example, "123" can be decoded as "ABC", "LC", or "AW", giving 3 possible decodings. 💡 Logic: We use a DP array dp[i] representing the number of ways to decode up to the i-th digit: If the current digit is not '0', it can stand alone → add dp[i-1]. If the last two digits form a number between 10 and 26, they can form a valid letter → add dp[i-2]. Base cases: dp[0] = dp[1] = 1 (a single non-zero digit has one decoding). This approach ensures O(n) time complexity with O(n) space — an elegant DP solution handling all edge cases like leading zeros or invalid pairs. #GeekStreak2025 #GFG160 #DSA #DynamicProgramming #ProblemSolving #StringDecoding #CodingChallenge #Python

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories