Warm Up-2

stringTimes

public String stringTimes(String str, int n) {
    String nStr = "";
    for (int i = 0; i < n; i++) {
        nStr = nStr + str;
    }
    return nStr;
}

frontTimes

public String frontTimes(String str, int n) {
     String front = "";
     if (n > str.length()) {
         for (int i = 0; i < n; i++) {
             front = front + str;
         }
     } else {
         for (int i = 0; i < n; i++) {
             front = front + str.substring(0, 3);
         }
     }
     return front;
 }

countXX

public int countXX(String str) {
       int count = 0;
       for (int i = 0; i < str.length(); i++) {
           if (str.indexOf("xx", i) == i) {
               count++;
           }
       }
       return count;
   }

doubleX

boolean doubleX(String str) {
        int a = str.indexOf("x");
        if (a != -1) {
            return a == str.indexOf("xx");
        }
        return false;
    }

stringBits

public String stringBits(String str) {
    String result = "";
    int n = 2;
    for (int i = 0; i < str.length(); i = i + n) {
        result = result + str.charAt(i);
    }
    return result;
}

stringSplosion

public String stringSplosion(String str) {
       String s1 = "";
       if (str.length() >= 4) {
           s1 = s1 + str.charAt(0) + str.charAt(0) + str.charAt(1);
           for (int i = 3; i < str.length(); i++) {
               s1 = s1 + str.substring(0, i);
           }
           return s1 + str;
       }
       if (str.length() <= 3 && str.length() > 2) { 
           s1 = s1 + str.charAt(0) + str.charAt(0) + str.charAt(1);
           return s1 + str;
       } else if (str.length() <= 2 && str.length() > 1) { //ab
           s1 = s1 + str.charAt(0) + str.charAt(0) + str.charAt(1);
           return s1;
       }
       return str;
   }

last2

public int last2(String str) {
       int count = 0;
       String s1 = "";
       if (str.length() > 1) {
           s1 = str.substring(str.length() - 2);
       }
       for (int i = 0; i < str.length() - 2; i++) {
           if (str.indexOf(s1, i) == i) {
               count++;
           }
       }
       return count;
   }

arrayCount9

public int arrayCount9(int[] nums) {
    int count = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == 9) {
            count++;
        }
    }
    return count;
}

arrayFront9

public boolean arrayFront9(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 9 && i < 4) {
                return true;
            }
        }
        return false;
    }

array123

public boolean array123(int[] nums) {
       for (int i = 0; i < nums.length; i++) {
           if (nums.length > i + 2 && nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3) {
               return true;
           }
       }
       return false;
   }

stringMatch

public int stringMatch(String a, String b) {
       String c = "";
       int count = 0;
       for (int i = 0; i + 1 < a.length(); i++) {
           if ((b.indexOf(c + a.charAt(i) + a.charAt(i + 1), i) == i)) {
               count++;
           }
       }
       return count;
   }

stringX

public String stringX(String str) {
    String a = "";
    if (str.length() > 2) {
        for (int i = 1; i + 1 < str.length(); i++) {
            if (str.charAt(i) != 120) {
                a = a + str.charAt(i);
            }
        }
        return str.substring(0, 1) + a + str.substring(str.length() - 1, str.length());
    }
    return str;
}

altPairs

public String altPairs(String str) {
      String a = "";
      if (str.length() > 2) {
          for (int i = 0; i < str.length(); i += 4) {
              a = a + str.charAt(i);
              if (i + 1 < str.length()) {
                  a = a + str.charAt(i + 1);
              }
          }
          return a;
      }
      return str;
  }

stringYak

public String stringYak(String str) {
       String a = "";
       for ( int i = 0; i < str.length(); i++ ) {
           if(i+3 < str.length() && str.substring(i, i+3).equals("yak")){
               i+=3;
           }else if (str.substring(i, str.length()).equals("yak") ){
               return a;
           }
           a = a + str.charAt(i);
       }
       return a;
   }

array667

public int array667(int[] nums) {
        int count = 0;
        for (int i = 0; i<nums.length; i++ ){
            if(nums.length>i+1 && nums[i] == 6 && nums[i+1] == 6){
                count++;
            }else if(nums.length>i+1 && nums[i] == 6 && nums[i+1] == 7 ){
                count++;
            }
        }
        return count;
    }

noTriples

public boolean noTriples(int[] nums) {
     for (int i = 0; i < nums.length; i++) {
         if (i + 2 < nums.length && nums[i] == nums[i + 1] && nums[i] == nums[i + 2]) {
             return false;
         }
     }
     return true;
 }

has271

public boolean has271(int[] nums) {
       for (int i = 0; i < nums.length - 2; i++) {
           if (nums[i + 1] == nums[i] + 5) {
               if (Math.abs((nums[i] - 1) - nums[i + 2]) <= 2) {
                   return true;
               }
           }
       }
       return false;
   }

Last updated