forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeFront.java
More file actions
20 lines (18 loc) · 695 Bytes
/
deFront.java
File metadata and controls
20 lines (18 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* Given a string, return a version without the first 2 chars. Except keep
* the first char if it is 'a' and keep the second char if it is 'b'. The
* string may be any length. Harder than it looks.
*/
public String deFront(String str) {
if(str.length() == 1 && str.charAt(0) != 'a')
return "";
if(str.length() >= 2) {
if(str.charAt(0) != 'a' && str.charAt(1) != 'b') {
return str.substring(2);
} else if(str.charAt(0) != 'a') {
return str.substring(1);
} else if(str.charAt(1) != 'b') {
return "a" + str.substring(2);
}
}
return str;
}