String-2 Codingbat Full Solutions

Answers to Coding Bat's String-2 Problems, all detailed and explained.

 doubleChar H  countHi H  catDog
 countCode endOther xyzThere
 bobThere xyBalance mixString
 repeatEnd repeatFront repeatSeparator
 prefixAgain xyzMiddle getSandwich
 sameStarChar zipZap starOut
 plusOut wordEnds


9 comments :

  1. for "oneTwo":

    public String oneTwo(String str) {
    String result = "";
    for (int i=0; i<str.length()-2; i+=3){
    result = result + str.substring(i+1, i+3) + str.charAt(i);
    }
    return result;
    }

    ReplyDelete
  2. public String oneTwo(String str) {
    String tmp = "";
    int len = str.length();

    for (int i = 0; i<len-2; i+=3){
    tmp += str.substring(i+1,i+3)+str.substring(i,i+1);


    }
    return tmp;
    }

    ReplyDelete
  3. Recursion for "String-2 > oneTwo"

    public String oneTwo(String str) {
    if( str.length() < 3 ) return "";
    return ""+str.charAt(1)+str.charAt(2)+str.charAt(0)+oneTwo(str.substring(3));
    }

    ReplyDelete
  4. public String oneTwo(String str) {
    String a = "";
    for(int i = 0; i < str.length()-2; i+=3){
    a = a + str.charAt(i+1) + str.charAt(i+2)+ str.charAt(i);
    }
    return a;
    }

    ReplyDelete
  5. public boolean xyBalance(String str) {
    int len=str.length();
    boolean r= true;
    if(len>=1){



    for(int i=0;i<len;i++){
    if(str.charAt(i)=='x'){
    r=false;
    for(int j=i+1;j<len;j++)
    if(str.charAt(j)=='y')
    r=true;
    }


    }

    }
    return r;
    }

    ReplyDelete

  6. Given a string, compute a new string by moving the first char to come after the next two chars, so "abc" yields "bca". Repeat this process for each subsequent group of 3 chars, so "abcdef" yields "bcaefd". Ignore any group of fewer than 3 chars at the end.


    public String oneTwo(String str) {
    String str1="";
    if(str.length()>2)
    {
    for(int i=0;i<str.length()-2;i=i+3)
    str1+=str.substring(i+1,i+3)+str.charAt(i);
    }
    return str1;
    }

    ReplyDelete
  7. public String oneTwo(String str) {
    int len = str.length();
    String[] arr = new String[len/3];
    if (len % 3 != 0) {
    int mod1 = len % 3;
    str = str.substring(0,len-mod1);
    }
    int j = 0;
    for (int i = 0; i < len/3; i++) {
    arr[i] = str.substring(j,j+3);
    j += 3;
    }
    String strAll="";
    for (int i = 0; i < len / 3; i++) {
    strAll += arr[i].substring(1,3) + arr[i].substring(0,1);
    }
    return strAll;
    }

    ReplyDelete
  8. Coding Question Mark For Review
    Problem Statement

    Given an array count the number of even elements in an array.

    Complete the functions countEvens() to get the expected output.

    Only edit the function and not the existing code.



    Note

    Edit only the function and not the existing code.

    Sample Testcases

    countEvens([2, 1, 2, 3, 4]) → 3
    countEvens([2, 2, 0]) → 3
    countEvens([1, 3, 5]) → 0

    --------------------------------------------------------------------------------

    The above mentioned example is a sample test case for your understanding. The program will be tested on other secret test cases in the backend.Output format should be maintained to be scored properly.Output format should be maintained to be scored properly Make sure you click the SUBMIT button to save and submit your answer.

    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