Given a string, if the string "del" appears starting at index 1, return a string where that "del" has been deleted. Otherwise, return the string unchanged.
delDel("adelbc") → "abc" delDel("adelHello") → "aHello" delDel("adedbc") → "adedbc" |
Hisoka's way :
1 2 3 4 5 | public String delDel(String str) { if(str.length() <= 3) return str; else if(str.substring(1, 4).equalsIgnoreCase("del"))return str.replace("del", ""); else return str; } |
Result:
Codingbat answer:
1 2 3 4 5 6 7 8 | public String delDel(String str) { if (str.length()>=4 && str.substring(1, 4).equals("del")) { // First char + rest of string starting at 4 return str.substring(0, 1) + str.substring(4); } // Otherwise return the original string. return str; } |
No comments:
Post a Comment