-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumNumberOfKeyPresses.java
More file actions
35 lines (26 loc) · 973 Bytes
/
minimumNumberOfKeyPresses.java
File metadata and controls
35 lines (26 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
The special keyboard has 11 numeric keys {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 00}. If he presses 00 , the previously displayed value will be multiplied by 100. Whereas, if he presses any other numeric key, the previously displayed value will first be multiplied by 10 and then the number on the key will be added to it.
example : "60004"
answer : 4
*/
public class minimumNumberOfKeyPresses {
public static int keyPresses(String value) {
int length = value.length();
int i = 0;
int keypresses = 0;
while (i < length) {
if (i < length - 1 && value.charAt(i) == '0' && value.charAt(i+1) == '0') {
keypresses += 1;
i += 2;
} else {
keypresses += 1;
i += 1;
}
}
return keypresses;
}
public static void main(String[] args) {
String value = "60004";
System.out.println(keyPresses(value));
}
}