문제
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
'알고리즘' 카테고리의 다른 글
2024 11 05 2914. Minimum Number of Changes to Make Binary String Beautiful (1) | 2024.11.05 |
---|---|
2024 11 04 3163. String Compression III (1) | 2024.11.04 |
2024 11 02 2490. Circular Sentence (0) | 2024.11.02 |
2024 11 04 1957. Delete Characters to Make Fancy String (2) | 2024.11.01 |
2024 10 30 1671. Minimum Number of Removals to Make Mountain Array (0) | 2024.10.30 |