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:

Software Engineering Concepts Software Maintenance Test Your Understanding

  Software Engineering Concepts       Software Maintenance            Test Your Understanding

Logic Development Logic Development Test Your Understanding

Logic Development       Logic Development            Test Your Understanding

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 | +------------------------------+

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

  Question   1 Correct Mark 1.00 out of 1.00 Flag question Question text State True or False. JSON is data oriented and does not provide display capabilities. Select one: True   False Feedback The correct answer is 'True'. Question  2 Incorrect Mark 0.00 out of 1.00 Flag question Question text Which one of the following indicators define that either one of the child element must occur within the element? Select one: choice sequence      all any Feedback Your answer is incorrect. The correct answer is: choice Question  3 Correct Mark 1.00 out of 1.00 Flag question Question text JSON object value is accessed by using ________. Select one: dot(.) notation   star(*) notation colon(:) notation dollar($) notation Feedback Your answer is correct. The correct answer is: dot(.) notation Question  4 Correct Mark 1.00 out of 1.00 Flag question Question text Which of the following is a well formed XML document? Select one: <?xml version=”1.0...

RDBMS Data Definition Language | Create Distributor table

  Write a query to create Distributor table with constraints mentioned.  Refer the below schema  Result Description Summary of tests +------------------------------+ | 3 tests run / 3 test passed | +------------------------------+

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  

Web Technology HTML 5 HTML 2 | Quiz 1

 Web Technology       HTML 5            HTML 2               Quiz 1  

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.

Accenture TFA Quiz | Part 1

  Question  1 Correct Marked out of 1 Flag question Question text The employee table contains EmployeeNumber, EmployeeName, Salary and DeptCode Columns. Which is the CORRECT SQL query to display DeptCode and average salary of each department? Choose most appropriate option. Select one: a.  SELECT DeptCode,avg(salary) FROM Employee GROUP BY DeptCode;   b.  SELECT DeptCode,avg(salary) FROM Employee GROUP BY EmployeeNumber; c.  SELECT DeptCode,avg(salary) FROM Employee; d.  SELECT DeptCode,avg(salary) FROM Employee GROUP BY Salary; Feedback The correct answer is: SELECT DeptCode,avg(salary) FROM Employee GROUP BY DeptCode; Question  2 Correct Marked out of 1 Flag question Question text Which is the CORRECT SQL Query to display names of employees which has only 3 letters that starts with 'A' and ends with 'e'? Choose most appropriate option. Select one: a.  SELECT EmployeeName FROM Employee WHERE EmployeeName LIKE 'A_e';   b.  SELECT Em...

Subscribe to Get's Answer by Email