旋转字符串:
给定两个字符串, s 和 goal。如果在若干次旋转操作之后,s 能变成 goal ,那么返回 true 。
s 的 旋转操作 就是将 s 最左边的字符移动到最右边。
例如, 若 s = ‘abcde’,在旋转一次之后结果就是’bcdea’ 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-string
输入: s = “abcde”, goal = “cdeab”
输出: true
//循环拼接字符串并判断
public class RotateString {
public static boolean rotateString(String s, String goal) {
if(s.equals(goal)){
return true;
}
if (s.length() != goal.length()){
return false;
}
for(int i = 0; i < s.length();i++) {
if (s != null){
String a = "" + s.charAt(0);
String res = s.substring(1,s.length());
s = res + a;
}
if (s.equals(goal)){
return true;
}
}
return false;
}
public static void main(String[] args) {
System.out.println(rotateString("abcde","cdeab"));
}
}