Well, this algorithm can be at least improved by adding option to check for any list characters at position 0 and 1.
public class DeFront {
//if character is one of these, it will be removed
private static char[] firstLetterExcluded = "a".toCharArray();
private static char[] secondLetterExcluded = "bcdef".toCharArray();
public static String deFront(String str) {
boolean removeFirst = false, removeSecond = false;
for (char c : firstLetterExcluded) {
if (str.charAt(0) == c) {
removeFirst = true;
break;
}
}
for (char c : secondLetterExcluded) {
if (str.charAt(1) == c) {
removeFirstremoveSecond = true;
break;
}
}
//Before calling the stripping method, it's better to check if we need to
if (removeFirst || removeSecond) {
return stripString(str, removeFirst, removeSecond);
} else {
return str;
}
}
private static String stripString(String str, boolean removeFirst, boolean removeSecond) {
if (removeFirst && removeSecond) {
return str.substring(2);
} else if (removeFirst) {
return str.substring(1);
} else {
//Using Stringbuilder takes more time
return str.charAt(0) + str.substring(2);
}
}
}