Problem:
Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.
withoutX("xHix") → "Hi"
withoutX("xHi") → "Hi"
withoutX("Hxix") → "Hxi"
Solution:
public String withoutX(String str) { if (str.length() == 0) return str; if (str.length() == 1){ if (str.charAt(0) == 'x') return ""; else return str; } if (str.charAt(0) == 'x') str = str.substring(1,str.length()); if (str.charAt(str.length()-1) == 'x') str = str.substring(0,str.length()-1); return str; }
ReplyDeleteMy fantasy world... f me.....
public String withoutX(String str) {
String sum="";
if(!str.contains("x")){
return str;
}
if(str.charAt(0)=='x'&&str.charAt(str.length()-1)!='x'){
sum=sum+str.substring(1);
}
else if(str.charAt(0)!='x'&&str.charAt(str.length()-1)=='x'){
sum=sum+str.substring(0,str.length()-1);
}
else if(str.charAt(0)=='x'&&str.charAt(str.length()-1)=='x'){
for(int i=1;i<str.length()-1;i++){
sum=sum+str.substring(i,i+1);
}
}
else if(str.charAt(0)!='x'&&str.charAt(str.length()-1)!='x'){
return str;
}
return sum;
}
I find the easiest-to-understand solutions minimize the number of return statements, this solution only uses one:
ReplyDeletepublic String withoutX(String str) {
StringBuilder result = new StringBuilder(str);
if (!str.isEmpty() && str.length() > 1 && str.charAt(str.length()-1) == 'x') {
result = result.deleteCharAt(str.length()-1);
}
if (!str.isEmpty() && str.charAt(0) == 'x') {
result = result.deleteCharAt(0);
}
return result.toString();
}
public String withoutX(String str) {
ReplyDeleteif(str.length()==0 || str.length()==1)
return "";
else if(str.charAt(0) == 'x' && str.charAt(str.length()-1) == 'x')
return str.substring(1,str.length()-1);
else if(str.charAt(0) == 'x')
return str.substring(1,str.length());
else if(str.charAt(str.length()-1) == 'x')
return str.substring(0,str.length()-1);
else return str;
}
public String withoutX(String str) {
ReplyDeleteint len = str.length();
String word = "";
for (int i = 0; i < len; i++){
if (i == 0 && str.charAt(0) != 'x') {
word += str.charAt(0);
} else if (i > 0 && i < len-1) {
word += str.charAt(i);
} else if (i == len-1 && str.charAt(len-1) != 'x') {
word += str.charAt(len-1);
}
}
return word;
}
if (str.length() <= 1)
ReplyDeletereturn "";
if (str.charAt(0) == 'x')
str = str.substring(1, str.length());
if (str.charAt(str.length()-1) == 'x')
str = str.substring(0, str.length()-1);
return str;
public String withoutX(String str) {
ReplyDeleteif(str.startsWith("x")) str = str.substring(1);
if(str.endsWith("x")) str = str.substring(0,str.length()-1);
return str;
}
if (str.length()>=1 && str.charAt(0) == 'x')
ReplyDeletestr = str.substring(1,str.length());
if (str.length()>=1 && str.charAt(str.length()-1) == 'x')
str = str.substring(0,str.length()-1);
return str;
public String withoutX(String str) {
ReplyDeleteif(str.equals("x")){
return "";
}
if(str.startsWith("x") && str.endsWith("x")){
return str.substring(1, str.length() -1);
}
if(str.startsWith("x")){
return str.substring(1);
}
if(str.endsWith("x")){
return str.substring(0, str.length() - 1);
}
return str;
}
public String repeatFront(String str, int n) {
ReplyDeletereturn (n == 0 || str.isEmpty())? "":
str.substring(0, n) + repeatFront(str, n - 1);
}
public String withoutX(String str) {
ReplyDeleteif(str.length()<2){
return "";
}
else if(str.substring(0,1).equals("x")&&str.substring(str.length()-1,str.length()).equals("x")){
return str.substring(1,str.length()-1);
}
else if(str.substring(0,1).equals("x")){
return str.substring(1);
}
else if (str.substring(str.length()-1,str.length()).equals("x")){
return str.substring(0,str.length()-1);
}
return str;
}
public String withoutX(String str) {
ReplyDeleteif(str.length()>1 && str.startsWith("x") && str.endsWith("x"))return str.substring(1,str.length()-1);
if(str.length()>1 && str.startsWith("H") && str.endsWith("x"))return str.substring(0,str.length()-1);
if(str.length()>1 && str.startsWith("x") && str.endsWith("i"))return str.substring(1,str.length());
else if(str.length()<2)return "";
return str;
}
public String withoutX(String str) {
ReplyDeleteif (str.length()>=1 && str.charAt(0) == 'x')
str = str.substring(1);
if (str.length()>=1 && str.charAt(str.length()-1) == 'x')
str = str.substring(0,str.length()-1);
return str;
}
In JavaScript:
ReplyDeletefunction withoutX(str){
if(str.length == 1) {
if(str[0] == 'x') {
return '';
} else {
return str;
}
}
if(str.startsWith('x') && str.endsWith('x')) {
str = str.substring(1, str.length - 1)
return str;
} else if(str[str.length - 1] == 'x') {
return str = str.substring(0, str.length - 1);
} else if(str[0] == 'x') {
return str = str.substring(1)
} else {
return str
}
}