Skip to main content
added 167 characters in body
Source Link
Joe F
  • 854
  • 5
  • 13

First, is this part correct?

} else if (str.length() == 1 && str.charAt(0) == 'a') {
    return str;
} else {
    return str;
}

This will return a string of length 1, unchanged, regardless of the character. What should happen if you pass "z"? Since the first character is not 'a', should it be removed?

I suspect it was supposed to be something like this:

} else if (str.length() == 1 && str.charAt(0) != 'a') {
    return "";
} else {
    return str;
}

The rest of this answer assumes it should remove the character

You could use a StringBuilder and delete the characters as necessary:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}

Note: The order of deletion is important in both of these solutions. The b is deleted first to prevent the deletion of a from shifting the b by one position.

First, is this part correct?

} else if (str.length() == 1 && str.charAt(0) == 'a') {
    return str;
} else {
    return str;
}

This will return a string of length 1, unchanged, regardless of the character. What should happen if you pass "z"? Since the first character is not 'a', should it be removed?

I suspect it was supposed to be something like this:

} else if (str.length() == 1 && str.charAt(0) != 'a') {
    return "";
} else {
    return str;
}

The rest of this answer assumes it should remove the character

You could use a StringBuilder and delete the characters as necessary:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}

First, is this part correct?

} else if (str.length() == 1 && str.charAt(0) == 'a') {
    return str;
} else {
    return str;
}

This will return a string of length 1, unchanged, regardless of the character. What should happen if you pass "z"? Since the first character is not 'a', should it be removed?

I suspect it was supposed to be something like this:

} else if (str.length() == 1 && str.charAt(0) != 'a') {
    return "";
} else {
    return str;
}

The rest of this answer assumes it should remove the character

You could use a StringBuilder and delete the characters as necessary:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}

Note: The order of deletion is important in both of these solutions. The b is deleted first to prevent the deletion of a from shifting the b by one position.

deleted 1 characters in body
Source Link
Joe F
  • 854
  • 5
  • 13

First, is this part correct?

} else if (str.length() == 1 && str.charAt(0) == 'a') {
    return str;
} else {
    return str;
}

This will return a string of length 1, unchanged, regardless of the character. What should happen if you pass "z"? Since the first character is not 'a', should it be removed?

I suspect it was supposed to be something like this:

} else if (str.length() == 1 && str.charAt(0) != 'a') {
    return "";
} else {
    return str;
}

The rest of this answer assumes this wasit should remove the intended behavior.character

You could use a StringBuilder and delete the characters as necessary:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}

First, is this part correct?

} else if (str.length() == 1 && str.charAt(0) == 'a') {
    return str;
} else {
    return str;
}

This will return a string of length 1, unchanged, regardless of the character. What should happen if you pass "z"? Since the first character is not 'a', should it be removed?

I suspect it was supposed to be something like this:

} else if (str.length() == 1 && str.charAt(0) != 'a') {
    return "";
} else {
    return str;
}

The rest of this answer assumes this was the intended behavior.

You could use a StringBuilder and delete the characters as necessary:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}

First, is this part correct?

} else if (str.length() == 1 && str.charAt(0) == 'a') {
    return str;
} else {
    return str;
}

This will return a string of length 1, unchanged, regardless of the character. What should happen if you pass "z"? Since the first character is not 'a', should it be removed?

I suspect it was supposed to be something like this:

} else if (str.length() == 1 && str.charAt(0) != 'a') {
    return "";
} else {
    return str;
}

The rest of this answer assumes it should remove the character

You could use a StringBuilder and delete the characters as necessary:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}
deleted 81 characters in body
Source Link
Joe F
  • 854
  • 5
  • 13

You could use a StringBuilder and delete the characters as necessary:First, is this part correct?

public String deFront(String str) {
  } else if (str.length() >= 2) {
        StringBuilder sb== =1 new&& StringBuilder(str);
        if (sb.charAt(10) !=== 'b''a') {
           return sb.deleteCharAt(1);str;
 } else {
    return str;
}
  

This will return a string of length 1, unchanged, regardless of the character. What should happen if you pass "z"? Since the first character is not 'a', should it be removed?

I suspect it was supposed to be something like this:

} else if (str.length() == 1 if&& (sbstr.charAt(0) != 'a') {
            sb.deleteCharAt(0);
        }
        return sb.toString();"";
    } else {
        return str;
    }
}

This only changes strings with length 2The rest of greater, asthis answer assumes this was the original code does. What should happen if you pass "z"? The first character is not 'a'intended behavior. Should it be removed? If so, you can check

You could use a StringBuilder and delete the length at each character removalcharacters as necessary:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}

You could use a StringBuilder and delete the characters as necessary:

public String deFront(String str) {
    if (str.length() >= 2) {
        StringBuilder sb = new StringBuilder(str);
        if (sb.charAt(1) != 'b') {
            sb.deleteCharAt(1);
        }
        if (sb.charAt(0) != 'a') {
            sb.deleteCharAt(0);
        }
        return sb.toString();
    } else {
        return str;
    }
}

This only changes strings with length 2 of greater, as the original code does. What should happen if you pass "z"? The first character is not 'a'. Should it be removed? If so, you can check the length at each character removal:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}

First, is this part correct?

} else if (str.length() == 1 && str.charAt(0) == 'a') {
    return str;
} else {
    return str;
}

This will return a string of length 1, unchanged, regardless of the character. What should happen if you pass "z"? Since the first character is not 'a', should it be removed?

I suspect it was supposed to be something like this:

} else if (str.length() == 1 && str.charAt(0) != 'a') {
    return "";
} else {
    return str;
}

The rest of this answer assumes this was the intended behavior.

You could use a StringBuilder and delete the characters as necessary:

public String deFront(String str) {
    StringBuilder sb = new StringBuilder(str);
    if (sb.length() > 1 && sb.charAt(1) != 'b') {
        sb.deleteCharAt(1);
    }
    if (sb.length() > 0 && sb.charAt(0) != 'a') {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

Or you could use a regular expression search & replace:

public String deFront(String str) {
    str = str.replaceFirst("^(.)[^b]", "$1");
    str = str.replaceFirst("^[^a]", "");
    return str;
}
added 548 characters in body
Source Link
Joe F
  • 854
  • 5
  • 13
Loading
Source Link
Joe F
  • 854
  • 5
  • 13
Loading