Algorithm/기타(기업등)

[CodingBat/Java] front22

IagreeBUT 2021. 9. 30. 01:49
728x90

 

링크

https://codingbat.com/prob/p183592

 

CodingBat Java Warmup-1 front22

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.

codingbat.com

 

 

문제

 

 

 

코드

 

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

 

 

public String front22(String str) {
  // First figure the number of chars to take
  int take = 2;
  if (take > str.length()) {
    take = str.length();
  }
  
  String front = str.substring(0, take);
  return front + str + front;
}
728x90