Skip to main content

Count Encodings

 1. You are given a string str of digits. (will never start with a 0)

2. You are required to encode the str as per following rules

    1 -> a

    2 -> b

    3 -> c

    ..

    25 -> y

    26 -> z

3. You are required to calculate and print the count of encodings for the string str.


     For 123 -> there are 3 encodings. abc, aw, lc

     For 993 -> there is 1 encoding. iic 

     For 013 -> This is an invalid input. A string starting with 0 will not be passed.

     For 103 -> there is 1 encoding. jc

     For 303 -> there are 0 encodings. But such a string maybe passed. In this case 

     print 0.

Input Format

A string str

Output Format

count of encodings


Constraints

0 < str.length <= 10

Sample Input

123

Sample Output

3


Solution:

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        
        int count = 1, n = str.length();
        
        for(int i=0;i<n;i++){
            int ch = str.charAt(i) - '0';
            if(ch >= 1 && ch <= 9){
                if((ch == 1 || ch == 2) && (i + 1 < n)){
                    int c = str.charAt(i+1) - '0';
                    c += ch*10;
                    if(c >= 10 && ch <= 26)
                        ++count;
                }   
            }else{
                System.out.println(0); 
                return;
            }
        }
        
        System.out.println(count);
    }
}

Comments

Must Read:

Data Formats ( XML & JSON ) XML AND JSON XML AND JSON | Quiz 2 & 3

Data Formats ( XML & JSON )  XML AND JSON  XML AND JSON Quiz 2 & 3  

Programming using Java Hands On - Control Structures | Print Customer Details

Print Customer Details Help Mr.Ben who is a programmer, in developing a registration form on console. Customers will not just input data, but also view the details once he/she finishes the registration.  Sample Input 1: Enter your name:Jany Enter age:25 Enter gender:Female Hailing from:Mexico Sample Output 1: Welcome, Jany Age:25 Gender:Female City:Mexico Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ | 5 tests run/ 5 tests passed | +------------------------------+

Data Formats ( XML & JSON ) XML AND JSON | Pre-Quiz

Data Formats ( XML & JSON )  XML AND JSON  Pre-Quiz  

RDBMS Data Definition Language | Drop Mobile Specification table (parent)

  RDBMS  Data Definition Language  Drop Mobile Specification table (parent) Refer the following schema and drop Mobile Specification table. (Hint: To drop parent table all associate tables need to be dropped or Use 'cascade constraints' command) Evaluation Result: Result Description Summary of tests +------------------------------+ | 1 tests run / 1 test passed | +------------------------------+

UNIX Introduction to Unix

  Calendar1 Display the previous, current and next month calendar of the  year 1987 august Calendar2 Display and see what is the specialty of September 1752. Calendar 5 Write a command to display the previous, current and next month calendar of the year 2015 December.

Min Cost In Maze Traversal

1. You are given a number n, representing the number of rows. 2. You are given a number m, representing the number of columns. 3. You are given n*m numbers, representing elements of 2d array a, which represents a maze. 4. You are standing in top-left cell and are required to move to bottom-right cell. 5. You are allowed to move 1 cell right (h move) or 1 cell down (v move) in 1 motion. 6. Each cell has a value that will have to be paid to enter that cell (even for the top-left and bottom-       right cell). 7. You are required to traverse through the matrix and print the cost of path which is least costly. Input Format A number n A number m e11 e12.. e21 e22.. .. n * m number of elements Output Format The cost of least costly path. Constraints 1 <= n <= 10^2 1 <= m <= 10^2 0 <= e1, e2, .. n * m elements <= 1000 Sample Input 6 6 0 1 4 2 8 2 4 3 6 5 0 4 1 2 4 1 4 6 2 0 7 3 2 2 3 1 5 9 2 4 2 7 0 8 5 1 Sample Output 23 Solution: import java.io.*; import...

Programming using Java Hands On - Control Structures | Check for Leap Year

  Given a year, check if the year is leap year or not.  If yes, the output should be “Leap Year”.  Else output should be “Not a Leap Year”.  The input should be a positive four digit number.  Else,  the output should be “Invalid Year”. Sample Input  1 : Enter the Year 2016 Sample Output  1 : Leap Year Sample Input  2 : Enter the Year 2001 Sample Output  2 : Not a Leap Year Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ | 12 tests run/12 tests passed | +------------------------------+

Programming using Java Hands On - Control Structures | Bill Generation

Bill Generation Tom went to a movie with his friends in a multiplex theatre and during break time he bought pizzas, puffs and cool drinks.  Consider   the following prices :  Rs.100/pizza Rs.20/puffs Rs.10/cooldrink Generate a bill for What Tom has bought. Sample Input 1: Enter the no of pizzas bought:10 Enter the no of puffs bought:12 Enter the no of cool drinks bought:5 Sample Output 1: Bill Details No of pizzas:10 No of puffs:12 No of cooldrinks:5 Total price=1290 ENJOY THE SHOW!!! Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ | 6 tests run/ 6 tests passed | +------------------------------+

Software Engineering Concepts Software Maintenance Test Your Understanding

  Software Engineering Concepts       Software Maintenance            Test Your Understanding

Subscribe to Get's Answer by Email