매일 Algorithm

신재원·2023년 1월 19일
0

Algorithm

목록 보기
11/243

프로그래머스 : 문자열 밀기 (LEVEL 0)

class Solution {
    public int solution(String A, String B) {
        int answer = 0;
        // "hello"	"ohell"	1
        // 정방향
        String tempA = A;
        for (int i = 0; i < A.length(); i++) {
            if(tempA.equals(B)){
                return answer;
            }
            // substring(설정 X,문자열.length)를 하면 끝 글자 한글자만 반환한다.
            // 'o'
            String tempASubString = tempA.substring(tempA.length() - 1);
            // index 0 부터 , 끝 한 글자 빼고 반환, tempA에 담아주어서 for문 반복
            // o + hell
            tempA = tempASubString + tempA.substring(0, tempA.length() - 1);

            answer ++;

        }
        return -1;
    }
}

0개의 댓글