jju_developer 2022. 12. 20. 21:03
728x90

<코딩 테스트>

문자열 "hello"에서 각 문자를 오른쪽으로 한 칸씩 밀고 마지막 문자는 맨 앞으로 이동시키면 "ohell"이 됩니다. 이것을 문자열을 민다고 정의한다면 문자열 A와 B가 매개변수로 주어질 때, A를 밀어서 B가 될 수 있다면 몇 번 밀어야 하는지 횟수를 return하고 밀어서 B가 될 수 없으면 -1을 return 하도록 solution 함수를 완성해보세요.

<나의 풀이 과정>

우선 예시를 apple로 했을 때,

A 는 hello

B 는 ohello 

만약 B를 두번 반복하게 된다면 ohelloohello 입니다.

이때  ohelloohello  인덱스 중 A가 없다면 -1 return 하도록 코딩하였습니다.

 

indexOf("찾는문자")

찾는 문자를 ("  ") 안에 추가

 

<나의 풀이 코드>

class Solution {
    public int solution(String A, String B) {
		String tempB = B.repeat(2);
		return tempB.indexOf(A);
	}
}

 

<다른 사람 풀이>

class Solution {
    public int solution(String A, String B) {
        int answer = -1;
        String temp = A;
        for(int i = 0 ; i < A.length() ; i++){
            if(temp.equals(B)){
                answer = i;
                break;
            }
            temp = temp.charAt(A.length()-1) + temp.substring(0, A.length()-1);

        }
        return answer;
    }
}

<다른 사람 풀이>

class Solution {
    public int solution(String A, String B) {
        if(A.equals(B)){
            return 0;
        }
        int count=0;
        while(count < A.length()){
            A = A.charAt(A.length()-1)+A.substring(0, A.length()-1);
            ++count;
            if(A.equals(B)){
                return count;
            }
        }
        return -1;
    }
}

 

 

 

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/120913

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

728x90