返回介绍

solution / 0700-0799 / 0796.Rotate String / README_EN

发布于 2024-06-17 01:03:34 字数 2842 浏览 0 评论 0 收藏 0

796. Rotate String

中文文档

Description

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.

 

Example 1:

Input: s = "abcde", goal = "cdeab"
Output: true

Example 2:

Input: s = "abcde", goal = "abced"
Output: false

 

Constraints:

  • 1 <= s.length, goal.length <= 100
  • s and goal consist of lowercase English letters.

Solutions

Solution 1

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;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文