Sunday, 25 September 2016

repeatFront

Home
Goto Problem
Given a string and an int n, return a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the length of the string, inclusive (i.e. n >= 0 and n <= str.length()).
repeatFront("Chocolate", 4) → "ChocChoChC"
repeatFront("Chocolate", 3) → "ChoChC"
repeatFront("Ice Cream", 2) → "IcI"

public String repeatFront(String str, int n) 
{
     String res=str.substring(0,n);
     for(int i=1;i<n;i++)
     {
            res=res+str.substring(0,n-i);
     }
     return res;
}

Saturday, 10 September 2016

loneSum

Goto Problem

Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.loneSum(1, 2, 3) → 6
loneSum(3, 2, 3) → 2
loneSum(3, 3, 3) → 0

public int loneSum(int a, int b, int c) {
if(a==b) { if(a==c) return 0; else return c; }
if(a==c) { return b; } if(b==c) return a; else return a+b+c; }

Monday, 5 September 2016

repeatSeparator

Home
Goto Problem

Given two strings, word and a separator, return a big string made of count occurences of the word, separated by the separator string.
repeatSeparator("Word", "X", 3) → "WordXWordXWord"
repeatSeparator("This", "And", 2) → "ThisAndThis"
repeatSeparator("This", "And", 1) → "This"

 public String repeatSeparator(String word, String sep, int count) 
 {
    String res=word;
    if(count==0) 
       return "";
   for(int i=1;i<count;i++)
   {
      res=res+sep;
      res=res+word;
   }
   return res;
}

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;
}