알고리즘
2024 11 03 Result796. Rotate String
물빠진떡
2024. 11. 3. 18:31
문제
Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
A shift on s consists of moving the leftmost character of s to the rightmost position.
For example, if s = "abcde", then it will be "bcdea" after one shift.
요약
첫글자를 마지막으로 돌리면서 goal과 같은게 나오는지 검사
풀이
처음에는 String.substring으로 풀긴했는데 효율이 너무 안좋게 나오길레
그냥 s 문자 안바꾸고 idx로 검하였다.
코드
class Solution {
public boolean rotateString(String s, String goal) {
if (s.length() != goal.length()){
return false;
}
for (int shift = 0; shift < s.length() ; shift ++) {
if (isSame(shift, s, goal)) {
return true;
}
}
return false;
}
public boolean isSame(int shift, String s, String goal) {
for (int gi = 0; gi < goal.length(); gi ++) {
int si = shift + gi < goal.length() ? shift + gi : shift + gi - goal.length();
if (s.charAt(si) != goal.charAt(gi)) {
return false;
}
}
return true;
}
}
풀이 시간
3분
체감 난이도
easy