public static String qMarkOut(String str) {
String result = "";
boolean inQuestion = false;
boolean chopable = true;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == '?') {
if (!inQuestion && chopable && result.length() > 0) {
result = result.substring(0, result.length() - 1);
chopable = false;
}
inQuestion = true;
}
else {
if (!inQuestion) {
result += str.charAt(i);
chopable = true;
}
inQuestion = false;
}
return result;
}
Function Call | Return Value | |||
---|---|---|---|---|
qMarkOut("aaa?bbb") | → | |||
qMarkOut("bb?ba") | → | |||
qMarkOut("aa") | → | |||
qMarkOut("a?") | → | |||
qMarkOut("?") | → | |||
qMarkOut("abc?d?ef") | → | |||
qMarkOut("hel?l?llo") | → | |||
qMarkOut("H??????ihi") | → | |||
qMarkOut("?") | → | |||
qMarkOut("") | → | |||
qMarkOut("a") | → |
Experiment with this code on Gitpod.io