• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Count Possible Decodings of a given Digit Sequence

February 10, 2015 by Dhaval Dave

Find number of ways a string can be decoded into, if A=1, B=2, C=3 … Z=25 and encoding number is 123 ways decoding can be done is
1 2 3 = A B C
1 23  = A X
12 3  = L C

How to find total number of decodings possible.
this is DP problem.
Start thinking in pattern.if
1 2 3 is String/Array input.

– 1 can be decoded in 1 way.
– 2 cab be decoded in 1 way.
– 1 2 (along with previous one < 26) so 2 can be decoded in (old number of way + 1) = 2 ways
– 3 can be decoded in 1 way
– 2 3 ( can be decoded also as < 26) so total = 3 ways.

Total ways = 3

Algo

input 
string A[N] 
NoOfWay[0]=1
NoOfWay[1]=2

for (i=1 to N){
   if (A[i] <= 26) NoOfWay [ i+1 ] = NoOfWay [ i ];
   if ( A[i] + (A[i-1]*10) <= 26 )  NoOfWay [ i+1 ] = NoOfWay [ i+1 ] + NoOfWay [ i – 1 ];
}
return NoOfWay [ N ]

Its very easy to code.
You can try ur self.
PS : We have assumed that number will be in range of 1-9.
You can edit algo/code to ensure numbers like 103 also. (0-9)
You can working code : http://ideone.com/2QA9Cq

 

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Dynamic Programming, Mathematical

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

CodeChef Code SGARDEN

Circular Linked List

Printing each word reverse in string

Binary Tree Isomorphic to each other

Daughter’s Age VeryGood Puzzle

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Client Server C program

SAP Off Campus Hiring_ March 2015 Verbal Skills

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Amazon Interview On-Campus For Internship – 1

The greedy coins game Dynamic Programming

Trapping Rain Water

Find Percentage of Words matching in Two Strings

Calculate price of parking from parking start end time prices

Find and print longest consecutive number sequence in a given sequence in O(n)

Search element in a matrix with all rows and columns in sorted order

Printing Longest Common Subsequence

Number of Islands BFS/DFS

Spanning Tree

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Printing intermediate Integers between one element & next element of array

Reverse a Linked List in groups of given size

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

ADOBE Aptitude C Language Test

Python Array String

Print all nodes that are at distance k from a leaf node

Find position of the only set bit

Maximum size of square sub matrix with all 1’s in a binary matrix

Copyright © 2025 · Genesis Framework · WordPress · Log in