LeetCode-796. Rotate String

网友投稿 688 2022-08-25

LeetCode-796. Rotate String

LeetCode-796. Rotate String

​​are given two strings, ​​A​​​ and ​​B​​.

A shift on ​​A​​​ consists of taking string ​​A​​​ and moving the leftmost character to the rightmost position. For example, if ​​A = 'abcde'​​​, then it will be ​​'bcdea'​​​ after one shift on ​​A​​​. Return ​​True​​​ if and only if ​​A​​​ can become ​​B​​​ after some number of shifts on ​​A​​.

Example 1:Input: A = 'abcde', B = 'cdeab'Output: trueExample 2:Input: A = 'abcde', B = 'abced'Output: false

Note:

​​A​​​ and​​B​​​ will have length at most​​100​​.

题解:暴力即可

class Solution {public: bool rotateString(string A, string B) { int a = A.length(), b = B.length(); if (a != b){ return false; } if (a == b && a == 0){ return true; } for (int i = 0; i < a; i++){ string C = A.substr(i, a - i); C += A.substr(0, i); if (C == B){ return true; } } return false; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-301. Remove Invalid Parentheses
下一篇:LeetCode-1091. Shortest Path in Binary Matrix
相关文章

 发表评论

暂时没有评论,来抢沙发吧~