-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdoorman.java
More file actions
56 lines (48 loc) · 957 Bytes
/
doorman.java
File metadata and controls
56 lines (48 loc) · 957 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
public class doorman {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int diff = scan.nextInt();
String str = scan.next();
int men = 0;
int women = 0;
while (true)
{
boolean hit = false;
if (Math.abs((men + 1) - women) <= diff)
{
if (str.length() > 0 && str.charAt(0) == 'M')
{
str = str.substring(1);
men++;
hit = true;
}
else if (str.length() > 1 && str.charAt(1) == 'M')
{
str = str.substring(0,1) + str.substring(2);
men++;
hit = true;
}
}
if (!hit && Math.abs(men - (women + 1)) <= diff)
{
if (str.length() > 0 && str.charAt(0) == 'W')
{
str = str.substring(1);
women++;
hit = true;
}
else if (str.length() > 1 && str.charAt(1) == 'W')
{
str = str.substring(0,1) + str.substring(2);
women++;
hit = true;
}
}
if (!hit || str.isEmpty())
break;
}
System.out.println(men + women);
scan.close();
}
}