给定两个字符串, s
和 goal
。如果在若干次旋转操作之后,s
能变成 goal
,那么返回 true
。
s
的 旋转操作 就是将 s
最左边的字符移动到最右边。
- 例如, 若
s = 'abcde'
,在旋转一次之后结果就是'bcdea'
。
示例 1:
输入: s = "abcde", goal = "cdeab" 输出: true
示例 2:
输入: s = "abcde", goal = "abced" 输出: false
提示:
1 <= s.length, goal.length <= 100
s
和goal
由小写英文字母组成
class Solution:
def rotateString(self, s: str, goal: str) -> bool:
return len(s) == len(goal) and goal in s + s
class Solution {
public boolean rotateString(String s, String goal) {
return s.length() == goal.length() && (s + s).contains(goal);
}
}
class Solution {
public:
bool rotateString(string s, string goal) {
return s.size() == goal.size() && strstr((s + s).data(), goal.data());
}
};
func rotateString(s string, goal string) bool {
return len(s) == len(goal) && strings.Contains(s+s, goal)
}
function rotateString(s: string, goal: string): boolean {
return s.length === goal.length && (goal + goal).includes(s);
}
impl Solution {
pub fn rotate_string(s: String, goal: String) -> bool {
s.len() == goal.len() && (s.clone() + &s).contains(&goal)
}
}
class Solution {
/**
* @param String $s
* @param String $goal
* @return Boolean
*/
function rotateString($s, $goal) {
return strlen($goal) === strlen($s) && strpos(($s.$s), $goal) !== false;
}
}