Showing posts with label String-2. Show all posts
Showing posts with label String-2. Show all posts

Thursday, 13 October 2016

repeatEnd

Home
Goto Problem

Given a string and an int N, return a string made of N repetitions of the last N characters of the string. You may assume that N is between 0 and the length of the string, inclusive.
repeatEnd("Hello", 3) → "llollollo"
repeatEnd("Hello", 2) → "lolo"
repeatEnd("Hello", 1) → "o"

public String repeatEnd(String str, int n)

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

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

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

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

Friday, 29 July 2016

plusout

Goto Problem

Given a string and a non-empty word string, return a version of the original String where all chars have been replaced by pluses ("+"), except for appearances of the word string which are preserved unchanged.

plusOut("12xy34", "xy") → "++xy++"
plusOut("12xy34", "1") → "1+++++"
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"

public String plusOut(String str, String word)
 {
    String res="";
    int i=0;
    while(i<str.length())
    {
       if(str.substring(i).startsWith(word))
       {
           res=res+word;
           i=i+word.length();
       }
       else 
       {
          res=res+"+";i++;
       }
    }
    return res;
 }

Sunday, 24 July 2016

countCode

Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2

public int countCode(String str)
{
   int count =0;
    for(int i=0;i<str.length()-3;i++)
   {
      if(str.charAt(i)=='c' && str.charAt(i+1)=='o' && str.charAt(i+3)=='e')
      count++;
   }
  return count;
 }

Friday, 22 July 2016

endOther

Goto Problem
Given two strings, return true if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note: str.toLowerCase() returns the lowercase version of a string.

endOther("Hiabc", "abc") → true
endOther("AbC", "HiaBc") → true
endOther("abc", "abXabc") → true

public boolean endOther(String a, String b)
{
   String al=a.toLowerCase(),bl=b.toLowerCase();
   return(al.endsWith(bl) || bl.endsWith(al));
}

Tuesday, 19 July 2016

xyBalance

Home

Goto Problem

We'll say that a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string. So "xxy" is balanced, but "xyx" is not. One 'y' can balance multiple 'x's. Return true if the given string is xy-balanced.

xyBalance("aaxbby") → true
xyBalance("aaxbb") → false
xyBalance("yaaxbb") → false
public boolean xyBalance(String str)
{
 int i=0;
 boolean res=true;
 while(i<str.length())
 {
 if(str.charAt(i)=='x')
 {
  for(int j=i;j<str.length();j++)
  {
      if(str.charAt(j)=='y')
     {
        res=true;
        break;
      }
    else
         res=false;
  }
   }
 i++;
  }
 return res;
 }

Sunday, 5 June 2016

wordEnds

Goto Problem

Given a string and a non-empty word string, return a string made of each char just before and just after every appearance of the word in the string. Ignore cases where there is no char before or after the word, and a char may be included twice if it is between two words.

wordEnds("abcXY123XYijk", "XY") → "c13i"
wordEnds("XY123XY", "XY") → "13"
wordEnds("XY1XY", "XY") → "11"
public String wordEnds(String str, String word)
 {
    String res="";
    if(word.equals(str))
       return res;
    if(str.startsWith(word)) 
    res=res+str.charAt(word.length());
    int i=1;
    while(i<(str.length()-word.length()))
   {
      if(str.substring(i).startsWith(word))
      {
         res=res+str.charAt(i-1)+str.charAt(i+word.length()); i=i+word.length();
       }
      else
       {
          i++;
       }
    }
    if(str.endsWith(word))
    {
        res=res+str.charAt(str.length()-word.length()-1);
        }
     return res;
 }