본문 바로가기

Algorithm/기타(기업등)

[CodingBat/Java] front22

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

'Algorithm > 기타(기업등)' 카테고리의 다른 글

[CodingBat/java] luckySum  (0) 2021.10.07
[CodingBat/Java] countXX  (0) 2021.09.30
[CodingBat/Java] missingChar  (0) 2021.09.24
[SAP/C++] BST from postorder  (0) 2021.04.24
[SAP/C++] Mirror Tree  (0) 2021.04.24