Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
| [762. 二进制表示中质数个计算置位](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/solution/by-ac_oier-w50x/) | 简单 | 🤩🤩🤩🤩 |
| [766. 托普利茨矩阵](https://leetcode-cn.com/problems/toeplitz-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/toeplitz-matrix/solution/cong-ci-pan-du-qu-cheng-ben-fen-xi-liang-f20w/) | 简单 | 🤩🤩🤩 |
| [794. 有效的井字游戏](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/solution/gong-shui-san-xie-fen-qing-kuang-tao-lun-pikn/) | 中等 | 🤩🤩🤩🤩 |
| [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/) | 简单 | 🤩🤩🤩 |
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
| [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 |
| [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 |
Expand Down
60 changes: 60 additions & 0 deletions LeetCode/791-800/796. 旋转字符串(简单).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
### 题目描述

这是 LeetCode 上的 **[796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/)** ,难度为 **简单**。

Tag : 「模拟」



给定两个字符串,`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` 由小写英文字母组成

---

### 模拟

由于每次旋转操作都是将最左侧字符移动到最右侧,因此如果 `goal` 可由 `s` 经过多步旋转而来,那么 `goal` 必然会出现在 `s + s` 中,即满足 `(s + s).contains(goal)`,同时为了 `s` 本身过长导致的结果成立,我们需要先确保两字符串长度相等。

代码:
```Java
class Solution {
public boolean rotateString(String s, String goal) {
return s.length() == goal.length() && (s + s).contains(goal);
}
}
```
* 时间复杂度:$O(n)$
* 空间复杂度:$O(n)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.796` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。