Search This Blog

Friday, August 16, 2013

Coding Bat Java String 1 solutions 19 to 25

This post contains the solutions to Java String-1 section of codingbat.com for problems 19 to 25. If you are new here, you can check my previous post learn-coding-computer-programming-for-beginners. Solutions to problems 1 to 18 are available in the link java-string1-part-1.Solutions to previous sections are also available check the links to java-warmup-1 , java-warmup-2.

19.       Problem Name : middleThree 
Solution:
Hint: Given a string of odd length, return the string length 3 from its middle, so "Candy" yields "and".
Constraint: The string length will be at least 3. 
public String middleThree(String str)
       {
                int len=str.length();
                if(len>3)
                {
                int middle=len/2;
                return(str.substring(middle-1,middle+2));
                }
                else
                 return str;
       }
20.       Problem Name : hasBad 
Solution:
Hint: Given a string, return true if "bad" appears starting at index 0 or 1 in the string, such as with "badxxx" or "xbadxx" but not "xxbadxx". Note: use .equals() to compare 2 strings. 
Constraint: The string may be any length, including 0
       public boolean hasBad(String str)
       {
               
                int len=str.length();
                if(len>3)
                {
                if(str.substring(0,3).equals("bad")||str.substring(1,4).equals("bad"))
                return true;
                else
                return false;
                }
                else if(len==3)
                {
                if(str.substring(0,3).equals("bad"))
                return true;
                else
                return false;
                }
                else
                return false;
               
       }
21.       Problem Name : atFirst 
Solution:
Hint: Given a string, return a string length 2 made of its first 2 chars.
Constraint: If the string length is less than 2, use '@' for the missing chars
public String atFirst(String str)
       {
                if(str.length()>2)
                       return (str.substring(0,2));
                else
                {
                       switch(str.length())
                       {
                       case 0:return("@@");
                    
                       case 1:return(str.substring(0,str.length())+"@");
                    
                       case 2:return (str.substring(0,str.length()));
                 
                       default:return("");
                       }
                }
       }
22.       Problem Name : lastChars
Solution:
Hint: Given 2 strings, a and b, return a new string made of the first char of a and the last char of b, so "yo" and "java" yields "ya".
Constraint: If either string is length 0, use '@' for its missing char. 
public String lastChars(String a, String b)
       {
                if(a.length()>1 && b.length()>1)
                {
                       return(a.substring(0,1)+b.substring(b.length()-1,b.length()));
                }
                else
                {
                       if(a.length()==0 && b.length()==0)
                             return "@@";
                       else if(a.length()==0)
                             return("@"+b.substring(b.length()-1,b.length()));
                       else
                             return(a.substring(0,1)+(b.length()<1?"@":b.substring(b.length()-1,b.length())));
                }
       }
23.       Problem Name : conCat
Solution:
Hint: Given two strings, append them together (known as "concatenation") and return the result.
Constraint: However, if the concatenation creates a double-char, then omit one of the chars, so "abc" and "cat" yields "abcat"
public String conCat(String a, String b)
       {
              if(a.length()>=1 && b.length()>=1)
              {
                if(a.substring(a.length()-1,a.length()).equals(b.substring(0,1)))
                       return (a.substring(0,a.length())+b.substring(1,b.length()));
                else
                       return (a.substring(0,a.length())+b.substring(0,b.length()));
               }
               else if(b.length()==0)
                      return (a.substring(0,a.length()));
               else
                      return(b.substring(0,b.length()));
             
       }

24.       Problem Name : lastTwo
Solution:
Hint: Given a string of any length, return a new string where the last 2 chars, if present, are swapped, so "coding" yields "codign". 
public String lastTwo(String str)
       {
                int len=str.length();
                if(len>2)
                {
                       return (str.substring(0,len-2)+str.substring(len-1,len)+str.substring(len-2,len-1));
                }
                else if(len==2)
                {
                       return(str.substring(1,str.length())+str.substring(0,str.length()-1)) ;
                }
                else
                {
                       return str;
                }
       }
25.       Problem Name : seeColor
Solution:
Hint: Given a string, if the string begins with "red" or "blue" return that color string, otherwise return the empty string.

public String seeColor(String str)
       {
              int len=str.length();
             
              if(len>=3)
              {
                if(str.substring(0,3).equals("red"))
                       return "red";
                else if(str.substring(0,3).equals("blu"))
                {
                       if(len==3)
                            return "";
                       else
                            return "blue";
                }
                  else
                      return "";
              }
              else
              {
                     return"";
              }
       }

Please check my posts for solutions to the remaining problems of Java String 1 section. 

No comments:

Post a Comment

Labels