Sunday 28 August 2016

prefixAgain

Home
Goto Problem
Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().
prefixAgain("abXYabc", 1) → true
prefixAgain("abXYabc", 2) → true
prefixAgain("abXYabc", 3) → false

public boolean prefixAgain(String str, int n)
{
   String prefix=str.substring(0,n);
   return(str.substring(n).contains(prefix));

}

Friday 26 August 2016

getSandwich

Home
Goto Problem

A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread.
getSandwich("breadjambread") → "jam"
getSandwich("xxbreadjambreadyy") → "jam"
getSandwich("xxbreadyy") → ""

public String getSandwich(String str)

{
  if(str.indexOf("bread")==str.lastIndexOf("bread")) 
      return "";
  return(str.substring(str.indexOf("bread")+5,str.lastIndexOf("bread")));
}

Tuesday 23 August 2016

sameStarChar

Home
Goto Problem

Returns true if for every '*' (star) in the string, if there are chars both immediately before and after the star, they are the same.
sameStarChar("xy*yzz") → true
sameStarChar("xy*zzz") → false
sameStarChar("*xa*az") → true

public boolean sameStarChar(String str) {
  int i=1;
  while(i<str.length()-1){
  if(str.charAt(i)=='*'){
  if(str.charAt(i-1)!=str.charAt(i+1))
  return false;
  }
  i++;
  
  }
  return true;
  }

Tuesday 9 August 2016

xyzMiddle

Goto Problem

Given a string, does "xyz" appear in the middle of the string? To define middle, we'll say that the number of chars to the left and right of the "xyz" must differ by at most one. This problem is harder than it looks.

xyzMiddle("AAxyzBB") → true
xyzMiddle("AxyzBB") → true
xyzMiddle("AxyzBBB") → false

public boolean xyzMiddle(String str) 
{
  int i=0;
  while(str.indexOf("xyz",i)!=-1)
 {
    i=str.indexOf("xyz",i);
    if(Math.abs(str.substring(0,i).length()-str.substring(i+3).length())<=1)
       return true;
   i++;
 }
 return false;
}

Wednesday 3 August 2016

zipZap

Home
Goto 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"

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

Monday 1 August 2016

starOut

Goto Problem

Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad".

starOut("ab*cd") → "ad"
starOut("ab**cd") → "ad"
starOut("sm*eilly") → "silly"

public String starOut(String str)
{ 
   String result = "";
   for(int i = 0; i < str.length(); i++)  
   {     
     if(str.charAt(i)=='*')     
     {
     }      
     else if(i != 0 && str.charAt(i-1)=='*')       
     { 
     }
     else  if(i != str.length() - 1 && str.charAt(i+1)=='*')
     {
     }    
     else 
     {      
          result += str.charAt(i);      
     }   
  }
     return result;   
}