-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP145.java
More file actions
40 lines (33 loc) · 1.19 KB
/
P145.java
File metadata and controls
40 lines (33 loc) · 1.19 KB
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
36
37
38
39
40
package p100_199;
import java.util.Scanner;
import java.util.Stack;
//accepted
public class P145 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int contadorParejas=0;
Stack<Character> pasajeros = new Stack<>();
String tren = sc.nextLine();
for (int i = 0; i < tren.length(); i++) {
switch (tren.charAt(i)){
case 'H':
case 'h': pasajeros.push(tren.charAt(i));
break;
case 'M': if (!pasajeros.isEmpty() && pasajeros.pop()=='H'){
contadorParejas++;}
else pasajeros.push(tren.charAt(i));
break;
case 'm': if (!pasajeros.isEmpty() && pasajeros.pop()=='h'){
contadorParejas++;
}
else pasajeros.push(tren.charAt(i));
break;
case '@':pasajeros.clear();
break;
}
}
System.out.println(contadorParejas);
}
}
}