본문 바로가기
Java/Programmers

[Lv.0]문자열 밀기

by GLOWWW 2023. 1. 17.

문자열 밀기는 반복문을 수행하며 문자열을 계속해서 새로 만들어주며 비교하였다. 이 때, substring을 사용하여 문자열을 잘라서 새로 만들어주었다.

public class 문자열밀기 {
    public static int solution(String A, String B) {
        int answer = 0;
        String A_copy = A;
        for (int i = 0; i < A.length(); i++) {
            if (A_copy.equals(B)) {
                return answer;
            }
            String tmp = A_copy.substring(A.length() - 1);
            A_copy = tmp + A_copy.substring(0, A.length() - 1);
            answer++;
        }
        return -1;
    }

    public static void main(String[] args) {
        int result = solution("hello", "elloh");
        System.out.println(result);
    }
}

'Java > Programmers' 카테고리의 다른 글

[Lv.0]등수 매기기  (0) 2023.01.17
[Lv.0]유한소수 판별하기  (0) 2023.01.17
[Lv.0]직사각형 넓이 구하기  (1) 2023.01.17
[Lv.0]캐릭터의 좌표  (0) 2023.01.17
[Lv.0]삼각형의 완성조건(2)  (0) 2023.01.17

댓글