Search

Wednesday 7 June 2017

front22

Problem:

Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back, so "kitten" yields"kikittenki". If the string length is less than 2, use whatever chars are there.

Test Case:

front22("kitten") → "kikittenki"
front22("Ha") → "HaHaHa"
front22("abc") → "ababcab"

Solution:

public String front22(String str) {
  if(str.length()>1)
  {
    return str.substring(0,2)+str+str.substring(0,2);
  }
 
  return str+str+str;
}

No comments:

Post a Comment