Java > String-1 > withoutX2 (CodingBat Solution)

Problem:

Given a string, if one or both of the first 2 chars is 'x', return the string without those 'x' chars, and otherwise return the string unchanged. This is a little harder than it looks.

withoutX2("xHi") → "Hi"
withoutX2("Hxi") → "Hi"
withoutX2("Hi") → "Hi"


Solution:

public String withoutX2(String str) {
  String temp = "";

  for (int i = 0; i < str.length(); i++) {
    if (i == 0 && str.charAt(i) != 'x')
      temp += str.charAt(i);
    else if (i == 1 && str.charAt(i) != 'x')
      temp += str.charAt(i);
    else if (i > 1)
      temp += str.charAt(i);
  }
  
  return temp;
}


24 comments :

  1. OMG another solution with a loop when is not supposed to? GG!

    ReplyDelete
    Replies
    1. public String withoutX2(String str) {

      if(str.length()==0){
      return str;
      }

      if(str.length()==1){
      if(str.equals("x")){
      return "";
      }
      else{
      return str;
      }

      }

      return str.substring(0,2).replaceAll("x","")+str.substring(2);

      }

      Delete
  2. public String withoutX2(String str) {
    String r=str;
    if (str.length()>1 && str.charAt(1)=='x')
    r=str.substring(0,1)+str.substring(2);
    if (str.length()>0 && str.charAt(0)=='x')
    r=r.substring(1);
    return r;
    }

    ReplyDelete
  3. public String withoutX2(String str){
    if(str.length() > 1 && str.charAt(1) == 'x')
    str = str.substring(0, 1) + str.substring(2);
    if(str.length() > 0 && str.charAt(0) == 'x')
    str = str.substring(1);
    return str;
    }

    ReplyDelete
    Replies
    1. This is likely the solution the designer of the exercises wished to see. No loops, no use of StringBuilder, one return statement.

      Delete
    2. public String without2(String str) {
      if(str.length() > 1 && str.charAt(1) == 'x')
      str = str.substring(0, 1) + str.substring(2);
      if(str.length() > 0 && str.charAt(0) == 'x')
      str = str.substring(1);
      return str;
      }

      Delete
  4. Here is a shorter solution:

    public String withoutX2(String str)
    {
    String word = "";
    for(int i=0; i<str.length(); i++)
    {
    if((i==0||i==1) && str.charAt(i)=='x')
    continue;
    word += str.charAt(i);
    }
    return word;
    }

    ReplyDelete
  5. // if want to know how not to solve it correctly



    public String withoutX2(String str) {

    int i=0;
    char ch ;
    String temp ="";

    for( i=0;i<str.length();i++){
    ch = str.charAt(i);
    if( ch == 'x'){
    if(i<2){
    ch ='_';
    }
    }
    temp += ch;
    }

    return temp.replace("_","");

    }

    ReplyDelete
  6. public String withoutX2(String str) {
    StringBuilder str2 = new StringBuilder();
    for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) == 'x' && i < 2)
    continue;
    str2.append(str.charAt(i));
    }
    return str2.toString();
    }

    ReplyDelete
  7. public String withoutX2(String str) {
    if(str.length() == 0) {
    return "";
    }
    if(str.length() == 1 && str.charAt(0) == 'x'){
    return "";
    }
    String twoFirstLetter = str.substring(0, 2);

    int indexFirstX = twoFirstLetter.charAt(0) == 'x' ? 0 : -1;
    int indexSecondX = twoFirstLetter.charAt(1) == 'x' ? 1 : -1;

    if(indexFirstX != -1 && indexSecondX != -1){
    return str.substring(2);
    }
    if(indexFirstX != -1 && indexSecondX == -1){
    return str.substring(1);
    }
    if(indexFirstX == -1 && indexSecondX == 1){
    return twoFirstLetter.charAt(0) + str.substring(2);
    }
    return str;
    }

    ReplyDelete
  8. public String withoutX2(String str) {
    String result = "";
    if (str.equals("x") || str.equals("xx") || str.equals(result)) {
    return result;
    }
    if (str.length() > 1) {
    String tmp = str.substring(0, 2);
    tmp = tmp.replaceAll("x", "");
    result = tmp + str.substring(2);
    }
    return result;
    }

    ReplyDelete
  9. int count = 0;
    for (int i = 0; i < str.length()-1; i++ ){
    if (str.substring(i,i+2).equals("hi"))
    count++;
    }
    return count;

    ReplyDelete
  10. int count = 0;
    for (int i = 0; i < str.length()-1; i++ ){
    if (str.substring(i,i+2).equals("hi"))
    count++;
    }
    return count;

    ReplyDelete
  11. int count = 0;
    for (int i = 0; i < str.length()-1; i++ ){
    if (str.substring(i,i+2).equals("hi"))
    count++;
    }
    return count;

    ReplyDelete
  12. public String withoutX2(String str) {
    String withoutX= "";
    if (str.length() >2)
    {
    String firstX = str.substring(0,1);
    String secondX = str.substring(1,2);
    String lastX = str.substring(2);
    if(!firstX.equals("x"))
    {
    withoutX+=firstX;
    }
    if(!secondX.equals("x"))
    {
    withoutX+=secondX;
    }

    withoutX+=lastX;
    }
    else if (str.length()==1)
    {
    if (!str.substring(0,1).equals("x"))
    {
    withoutX+=str.substring(0,1);
    }
    }
    else if (str.length()==2)
    {
    if (!str.substring(0,1).equals("x"))
    {
    withoutX+=str.substring(0,1);
    }
    if (!str.substring(1,2).equals("x"))
    {
    withoutX+=str.substring(1);
    }
    }

    return withoutX;
    }

    ReplyDelete
  13. if(str.equals("x") || str.isEmpty()){
    return "";
    }
    String firstTwo = str.substring(0,2);
    String replacement = firstTwo.replaceAll("x","");

    if(firstTwo.contains("x")){
    return replacement + str.substring(2);

    }
    return str;

    ReplyDelete
  14. public String withoutX2(String str) {
    if(str.equals("x") || str.isEmpty()){
    return "";
    }
    if(str.substring(0, 2).contains("x")){
    return str.substring(0, 2).replaceAll("x","") + str.substring(2);
    }
    return str;
    }

    ReplyDelete
  15. public String withoutX2(String str) {
    if(str.equals("x") || str.isEmpty()){
    return "";
    }
    if(str.substring(0, 2).contains("x")){
    return str.substring(0, 2).replaceAll("x","") + str.substring(2);
    }
    return str;
    }

    ReplyDelete
  16. public String withoutX2(String str) {
    if(str.equals("x") || str.isEmpty()){
    return "";
    }
    if(str.substring(0, 2).contains("x")){
    return str.substring(0, 2).replaceAll("x","") + str.substring(2);
    }
    return str;
    }

    ReplyDelete
  17. public String withoutX2(String str) {
    if (str.length() <= 1) return "";
    String temp = str.substring(0, 2);
    str = str.substring(2);
    temp = temp.replace("x", "");
    return temp.concat(str);
    }

    ReplyDelete
  18. public String withoutX2(String str) {
    if(str.length()==0)
    return "";
    String rtr = "";
    if(str.charAt(0)!='x')
    rtr+=str.charAt(0);
    if(str.length()>1 && str.charAt(1)!='x')
    rtr+=str.charAt(1);
    if(str.length()>2)
    rtr = rtr + str.substring(2);

    return rtr;
    }

    ReplyDelete
  19. Another way

    public String withoutX2(String str) {
    if (str.isEmpty()) return "";
    if (str.startsWith("xx")) return str.substring(2);
    if (str.startsWith("x")) return str.substring(1);
    if (str.substring(1, 2).equals("x")) return str.substring(0, 1) + str.substring(2);
    return str;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact