-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithoutX2.java
More file actions
32 lines (25 loc) · 878 Bytes
/
withoutX2.java
File metadata and controls
32 lines (25 loc) · 878 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
public String withoutX2(String str) {
String result = "";
if (str.length() < 1 || str.equals("x")) {
return result;
}
if (str.substring(0,1).equals("x") && str.substring(1,2).equals("x")) {
result = str.substring(2);
}
if (str.substring(0,1).equals("x") && !str.substring(1,2).equals("x")) {
result = str.substring(1);
}
if (!str.substring(0,1).equals("x") && str.substring(1,2).equals("x")) {
result = str.substring(0,1) + str.substring(2);
}
if (!str.substring(0,1).equals("x") && !str.substring(1,2).equals("x")) {
result = str;
}
return result;
}
/*
Given a string, if one or both of the first 2 chars is 'x', return the string without those 'x' chars, and otherwise return the string unchanged. This is a little harder than it looks.
withoutX2("xHi") → "Hi"
withoutX2("Hxi") → "Hi"
withoutX2("Hi") → "Hi"
*/