Command Unit Counter

Easy
0/40
0 upvote

Problem statement

You are given a string S which consists only of digits from '1' to '9'. This string can be interpreted as a sequence of commands. A "command unit" can be either:

1) A single digit. 2) A pair of two consecutive digits that form a number less than or equal to 26.

Your task is to count the total number of valid command units (both single-digit and valid two-digit combinations) that can be found in the string.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains a single string S.


Output Format:
Print a single integer representing the total count of all valid command units.


Note:
The string S will only contain digits from '1' to '9'.

Every single digit in the string is always considered a valid command unit.

The total count is the sum of all single-digit units plus all valid two-digit units.
Sample Input 1:
123


Sample Output 1:
5


Explanation for Sample 1:
The command units are identified as follows:
-Single-digit units:'1', '2', '3'. (Count: 3)
-Two-digit units:
 -'12' forms the number 12. Since 12 <= 26, this is a valid unit.
 -'23' forms the number 23. Since 23 <= 26, this is a valid unit.
-Total valid units = 3 + 2 = 5.


Sample Input 2:
271


Sample Output 2:
3


Explanation for Sample 2:
-Single-digit units:'2', '7', '1'. (Count: 3)
-Two-digit units:
 -'27' forms the number 27. Since 27 > 26, this is **not** a valid unit.
 -'71' forms the number 71. Since 71 > 26, this is **not** a valid unit.
-Total valid units = 3 + 0 = 3.


Expected Time Complexity:
The expected time complexity is O(N), where N is the length of the string.


Constraints:
1 <= N <= 10^5
`S` consists only of digits '1' through '9'.

Time limit: 1 sec
Approaches (1)
Command Unit Counter
Time Complexity

The expected time complexity is O(N), where N is the length of the string.

Space Complexity
Code Solution
(100% EXP penalty)
Command Unit Counter
Full screen
Console