본문 바로가기

코딩테스트/Coding Bat

[코딩뱃] [자바] Warmup - 1단계 : backAround 문제

728x90
728x90

https://codingbat.com/prob/p161642

 

CodingBat Java Warmup-1 backAround

Given a string, take the last char and return a new string with the last char added at the front and back, so "cat" yields "tcatt". The original string will be length 1 or more.

codingbat.com



= 문제 번역 =

문자열이 주어지면 마지막 문자를 가져 와서 앞뒤에 마지막 문자가 추가 된 새 문자열을 반환합니다.

ex) "cat"은 "tcatt"를 산출합니다.

+) 원래 문자열의 길이는 1 이상입니다.

 

= 문제푸는 팁 =

문제 자체에서 문자열의 길이는 1이상이라고 주어져있기 때문에,

굳이 if로 str의 문자가 1이상일 때 라고 설정해주지 않아도 된다.

 

= 해설 =

public String backAround(String str) {
  String back = str.substring(str.length()-1);
  return back+str+back;
}

728x90