Search This Blog

Friday, August 16, 2013

Coding Bat Java String 1 solutions 26 to 33

This post contains the solutions to Java String-1 section of codingbat.com for problems 26 to 33. 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-1java-string1-part2.Solutions to previous sections are also available check the links to java-warmup-1 , java-warmup-2.

26.       Problem Name : frontAgain 
Solution:
Hint: Given a string, return true if the first 2 chars in the string also appear at the end of the string, such as with "edited". 
public boolean frontAgain(String str)
       {
                if(str.length()>2)
                {
                       if(str.substring(0,2).equals(str.substring(str.length()-2,str.length())))
                             return true;
                       else
                             return false;
                }
                else if(str.length()==2)
                       return true;
                else
                       return false;
       }

27.       Problem Name : minCat 
Solution:
Hint: Given two strings, append them together (known as "concatenation") and return the result
Constraint: However, if the strings are different lengths, omit chars from the longer string so it is the same length as the shorter string. So "Hello" and "Hi" yield "loHi". The strings may be any length. 
public String minCat(String a, String b)
       {
              if(a.length()>b.length())
                     return(a.substring(a.length()-b.length(),a.length())+b);
              else
                     return(a+b.substring(b.length()-a.length(),b.length()));

       }

28.       Problem Name : extraFront 
Solution:
Hint: Given a string, return a new string made of 3 copies of the first 2 chars of the original string.
Constraint: The string may be any length. If there are fewer than 2 chars, use whatever is there. 

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

29.       Problem Name : without2 
Solution:
Hint: Given a string, if a length 2 substring appears at both its beginning and end, return a string without the substring at the beginning, so "HelloHe" yields "lloHe".
Constraint: The substring may overlap with itself, so "Hi" yields "". Otherwise, return the original string unchanged. 
public String without2(String str)
       {
              int len=str.length();
                if(str.length()>2)
                {
                      if(str.substring(0,2).equals(str.substring(len-2,len)))
                            return(str.substring(2,len));
                      else
                            return str;
                }
                else if(str.length()==1)
                       return str;
                else
                       return "";
       }

30.       Problem Name : deFront 
Solution:
Hint: Given a string, return a version without the first 2 chars. except keep the first char if it is 'a' and keep the second char if it is 'b'.
Constraint: The string may be any length.
public String deFront(String str)
       {   
                int len=str.length();
                int flag=0;
                String result="";
               
                if(str.charAt(1)=='b')
                {
                       result=(str.charAt(1)+str.substring(2,len));
                       flag=1;
                }
               
                if(str.charAt(0)=='a')
                {
                  if(flag==1)
                  {
                     result=(str.charAt(0)+result);
                  }
                  else
                     {
                       result=str.charAt(0)+str.substring(2,len);
                     }
                  flag=1;
                }
               
                if(flag!=1)
               
                {
                result= str.substring(2,len);
                }
                return result;
       }

31.       Problem Name : startWord 
Solution:
Hint: Given a string and a second "word" string, we'll say that the word matches the string if it appears at the front of the string, except its first char does not need to match exactly.
Constraint: On a match, return the front of the string, or otherwise return the empty string. So, so with the string "hippo" the word "hi" returns "hi" and "xip" returns "hip". The word will be at least length 1. 
public String startWord(String str, String word)
       {
              int len=word.length();
              if(str.length()==0)
                     return new String("");
              if(len==1 ){
                     return str.substring(0, 1);
              }
              String str1="";

              if(str.length()>=word.length())
              {
               str1=str.substring(1,len);
              }
              else
              {
               str1="";
              }
              word=word.substring(1);
              if(word.equals(str1))
                     return str.substring(0, len);
              else
                     return new String("");

       }

32.       Problem Name : withoutX 
Solution:
Hint: Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged. 
public String withoutX(String str)
       {
                int len=str.length();
                String result="";
                if(len>2)
                {
                       if(str.charAt(0)=='x' && str.charAt(len-1)=='x')
                             result=str.substring(1,len-1);
                       else if(str.charAt(0)=='x' || str.charAt(len-1)=='x')
                       {
                             if(str.charAt(0)=='x')
                                    result=str.substring(1,len);
                             if(str.charAt(len-1)=='x')
                                    result=str.substring(0,len-1);
                       }
                       else
                             result=str;
               
                }
                else
                {
                if(!str.contains("x"))
                       result=str;
               
                }
                return result;
       }


33.       Problem Name : withoutX2 
Solution:
Hint: 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.
public String withoutX2(String str)
       {
                int len=str.length();
                String result="";
                if(str.length()>1)
                {
                       if(str.charAt(0)=='x' && str.charAt(1)=='x')
                       {
                             str=str.substring(1,1)+str.substring(2,str.length()); 
                             result=str;
                       }
                       else if(str.charAt(0)=='x')
                       {
                             str=str.substring(1,2)+str.substring(2,str.length()); 
                             result=str;
                       }
                       else if(str.charAt(1)=='x')
                       {
                             str=str.substring(0,1)+str.substring(2,str.length()); 
                             result=str;

                       }
                       else
                       {
                             result=str;
                       }
                }
                else
                {
                       result="";
                }

                  return result;
              }

Please check my posts for solutions to other codingbat.com sections. 

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. 

Labels