Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Decode Ways problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Decode Ways problem solution A message containing letters from A-Z can be encoded into numbers using the following mapping:

‘A’ -> “1”

‘B’ -> “2”

…

‘Z’ -> “26”

To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, “11106” can be mapped into:

“AAJF” with the grouping (1 1 10 6)

“KJF” with the grouping (11 10 6)

Note that the grouping (1 11 06) is invalid because “06” cannot be mapped into ‘F’ since “6” is different from “06”. Given a string s containing only digits, return the number of ways to decode it. The answer is guaranteed to fit in a 32-bit integer.

Leetcode Decode Ways problem solution

Leetcode Decode Ways problem solution in Python.

class Solution:
    def numDecodings(self, s: str) -> int:
        dic = {str(i):0 for i in range(100)}
        for i in range(11,27):
            dic[str(i)] = 1 
        for i in range(1,10):
            dic[str(i)] = 1
            dic["0"+str(i)] = 0
            dic[str(i)+"0"] = 0
        dic["00"],dic["10"],dic["20"] = 0,1,1
        if len(s) == 1:
            return dic[s]
        dp = [0]*len(s)
        dp[0] = dic[s[0]]
        dp[1] = dic[s[:2]]+dic[s[0]]*dic[s[1]]
        for k in range(2,len(s)):
            dp[k] = dp[k-1]*dic[s[k]]+dp[k-2]*dic[s[k-1:k+1]]
        return dp[-1]

Decode Ways problem solution in Java.

class Solution {
    public int numDecodings(String s) {
        int l = s.length();
        int[] dp = new int[l];
        if(s.charAt(l-1)=='0')
            dp[l-1]=0;
        else
            dp[l-1]=1;
        for(int i=l-2;i>=0;i--){
            int a=s.charAt(i)-'0';
            int b=s.charAt(i+1)-'0';
            if(a==0){
                dp[i]=0;
                continue;
            }
            if(a==0 || a>=3 && b==0){
                return 0;
            }else if(a>=3 || (a==2 && b>6)){
                dp[i]=dp[i+1];
            }else if(b==0){
                dp[i]=(i+2<l?dp[i+2]:1);  
            }else{
                dp[i]=dp[i+1]+(i+2<l?dp[i+2]:1);
            }
            
        }
        return dp[0];
    }
}

Problem solution in C++.

class Solution {
public:
    int numDecodings(string s) 
    {
        int count = 0;
        queue<int> q;
        q.push(0);
        int d = 0;
        while (!q.empty())
        {
            int d = 0;
            int i = q.front();
            q.pop();
            for (; i<s.length(); i++)
            {
                d *= 10;
                d += s[i] -'0';
                if (d > 0 &&  d < 27)
                {
                    if (i+1 == s.length())
                    {
                        count++;
                    }
                    else
                        q.push(i+1);
                }
                else
                    break;
            }
            
        }
        return count;
        
    }
};

Problem solution in C.

int numDecodings(char* s) {
	int len = strlen(s);
	int* map = (int*)calloc(len + 1, sizeof(int));
	map[len] = 1;
	int i = len - 1;
	while (i >= 0)
	{
		if (s[i] == '0') map[i] = 0;
		else if (i == len - 1) map[i] = 1;
		else if (s[i] == '1' || (s[i] == '2'&&s[i + 1] <= '6')) map[i] = map[i + 1] + map[i + 2];
		else  map[i] = map[i + 1];
		i--;
	}
	return map[0];
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes