Java > String-2 > zipZap (CodingBat Solution)

Problem:

Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".

zipZap("zipXzap") → "zpXzp"
zipZap("zopzop") → "zpzp"
zipZap("zzzopzop") → "zzzpzp"


Solution:

public String zipZap(String str) {
  int len = str.length();
  String finalString = "";
  
  for (int i = 0; i < len; i++) {
    finalString += str.substring(i,i+1);
    if (i > 0 && i < len-1) {
      if (str.charAt(i-1) == 'z' && str.charAt(i+1) == 'p')
        finalString = finalString.substring(0,finalString.length()-1);
    }
  }
  return finalString;
}


22 comments :

  1. public String zipZap(String str) {
    String first="";
    String last="";
    String temp="";
    for(int i=0;i<str.length()-2;i++){
    temp=str.substring(i,i+3);
    if(temp.charAt(0)=='z' && temp.charAt(2)=='p'){
    first=str.substring(0,i+1);
    last=str.substring(i+2);
    str=first+last;
    }
    }
    return str;
    }

    ReplyDelete
    Replies
    1. public String zipZap(String str) {
      String first="";
      String last="";
      String temp="";
      String SOLVE = "";
      for(int i=0;i<str.length()-2;i++){
      temp=str.substring(i,i+3);
      if(temp.charAt(0)=='z' && temp.charAt(2)=='p'){
      first=str.substring(0,i+1);
      last=str.substring(i+2);
      SOLVE=SOLVE+first+last;
      i = i+3;
      }
      SOLVE = SOLVE+ temp;
      i = i+3;
      }
      return SOLVE;
      }

      Delete
    2. Easy solution with StringBuilder

      public String zipZap(String str) {
      StringBuilder sb = new StringBuilder(str);
      for(int i = 0; i < sb.length() - 2; i++){
      if(sb.charAt(i) == 'z' && sb.charAt(i+2) == 'p'){
      sb.deleteCharAt(i+1);
      }
      }
      String resultString = sb.toString();
      return resultString;
      }

      Delete
    3. Faster :P

      public String zipZap(String str) {
      return str.replaceAll("z.p", "zp");
      }

      Delete
  2. Definitely the quickest way --

    public String zipZap(String str) {
    return str.replaceAll("z.p", "zp");
    }

    ReplyDelete
  3. String result = "";
    int i = 0;

    while (i < str.length())
    {
    if (i != 0 && i != str.length() - 1 && str.charAt(i - 1) == 'z' && str.charAt(i + 1) == 'p')
    {
    i++;
    }
    else
    {
    result += str.charAt(i);
    i++;
    }
    }

    return result;

    ReplyDelete
  4. public String zipZap(String str) {
    for(int i=0;i<str.length()-2;i++){
    if(str.charAt(i)=='z'&&str.charAt(i+2)=='p'){str=str.substring(0,i+1)+str.substring(i+2);}
    }
    return str;
    }

    ReplyDelete
  5. public String zipZap(String str) {

    String temp = "";
    int len = str.length();

    if(str.length() <= 2)
    {
    return str;
    }

    for(int i=0;i<len-2;i++)
    {
    char s1 = str.charAt(i);
    char s2 = str.charAt(i+2);

    if(s1 == 'z' && s2 == 'p')
    {
    temp = temp + str.charAt(i) + str.charAt(i+2);
    i = i+2;
    }
    else
    {
    temp = temp + str.charAt(i) ;
    }
    }

    if(str.charAt(len -3) != 'z')
    {
    temp = temp + str.charAt(len-2) + str.charAt(len-1);
    }
    return temp ;
    }

    ReplyDelete
  6. public String zipZap(String str) {
    for (int i=0; i<str.length()-2; i++){
    if(str.charAt(i) == 'z' && str.charAt(i+2) == 'p'){
    str = str.substring(0, i+1) + str.substring(i+2);
    }
    }
    return str;
    }

    ReplyDelete
  7. //Just to add something for fun

    public String zipZap(String str) {

    int[] indexArray = new int[str.length()];
    String newStr="";

    if(str.length()<3){
    return str;
    }

    for (int i = 0; i<str.length(); i++){
    indexArray[i]=0;
    }


    for (int i = 0; i<str.length()-2; i++){
    if(str.charAt(i)=='z' && str.charAt(i+2)=='p'){
    indexArray[i+1]=1;
    }
    }


    for (int i = 0; i<str.length(); i++){
    if(indexArray[i]==0){
    newStr=newStr+str.charAt(i);
    }
    }

    return newStr;
    }

    ReplyDelete
  8. for (int i=0 ; i<str.length() -2 ; i++)
    {
    if ( str.charAt(i)=='z' && str.charAt(i+2)=='p')
    str= str.substring(0,i+1)+str.substring(i+2);
    }
    return str;

    ReplyDelete
  9. Recursion :

    public String zipZap(String str) {

    if ( str.length() < 3 )
    // Since the length of zip , zap or any z.p is 3
    return str;

    if( str.charAt(0)=='z' && str.charAt(2)=='p' )
    return ""+str.charAt(0)+str.charAt(2)+zipZap(str.substring(3));
    return str.charAt(0)+zipZap(str.substring(1));
    }

    ReplyDelete
  10. public String zipZap(String str) {
    String change = "";
    for (int i = 0; i < str.length(); i++)
    {
    if (i + 2 < str.length() && str.charAt(i) == 'z' && str.charAt(i + 2) == 'p')
    {
    change += (str.substring(i, i+1) + str.substring(i + 2, i + 3));
    i += 2;
    }
    else
    {
    change += str.charAt(i);
    }
    }
    return change;
    }

    ReplyDelete
  11. public String zipZap(String str) {
    int l=str.length();
    String res="";
    if ( l<3 || (!str.contains("z") || !str.contains("p")) ) return str;
    else {
    for (int i=0; i<l; i++){
    if (str.charAt(i)=='z' && str.charAt(i+2)=='p') {
    res+="z";
    res+="p";
    i+=2;
    }
    else res= res+ str.charAt(i);
    }

    }
    return res;
    }

    ReplyDelete
  12. String s = "";

    if(str.length() < 2) return str;

    if(str.length() > 2)
    {
    for(int i = 1; i < str.length() - 1; i++)
    {
    if(str.substring(i-1,i).equals("z") && str.substring(i+1,i+2).equals("p"))
    {
    continue;
    }
    else s+=str.charAt(i);
    }
    }
    return str.substring(0,1)+s+str.substring(str.length()-1);

    ReplyDelete
  13. public String zipZap(String str) {
    String string = "";

    for(int i = 0; i < str.length(); i++){
    if ((i != 0 && i != str.length()-1) && //check i is not first or last char
    (str.charAt(i - 1) == 'z' && str.charAt(i + 1) == 'p')){
    } else {
    string += str.charAt(i);
    }
    }

    return string;
    }

    ReplyDelete
  14. public String zipZap(String str) {
    return str.replaceAll("z[\\w]p", "zp");
    }

    ReplyDelete
  15. public String zipZap(String str) {
    String a="";
    if(str.length()<=2) return str;
    for(int i=0;i<str.length()-2;i++){
    if(str.charAt(i)=='z' &&str.charAt(i+2)=='p'){
    i=i+2; a=a+"zp";
    }
    else{a=a+str.charAt(i);}
    }
    if(str.charAt(str.length()-3)=='z' &&str.charAt(str.length()-1)=='p')
    return a;
    return a+ str.substring(str.length()-2);
    }

    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