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.
The first line of input contains a single string S.
Print a single integer representing the total count of all valid command units.
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.
123
5
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.
271
3
-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.
The expected time complexity is O(N), where N is the length of the string.
1 <= N <= 10^5
`S` consists only of digits '1' through '9'.
Time limit: 1 sec
The expected time complexity is O(N), where N is the length of the string.