We 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: true Example 2:
Input: A = 'abcde', B = 'abced'
Output: false
Note: A and B will have length at most 100.
class Solution {
public boolean rotateString(String A, String B) {
if(A == null || B == null || A.length() != B.length()){
return false;
}
if(A.length() == 0 && B.length() == 0){
return true;
} int n = A.length();
     //轮数
for(int i = 0; i< n; i++){
boolean find = true;
       //每一个元素
for(int j = 0; j < n; j++){
int a = A.charAt(j);
int newIndex = (n - i + j) % n;
int b = B.charAt(newIndex);
if(a != b){
find = false;
break;
}
}
if(find){
return true;
}
}
return false;
}
}

LeetCode - Rotate String的更多相关文章

  1. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  2. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  3. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  4. 796. Rotate String - LeetCode

    Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...

  5. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  6. Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

  7. Lintcode: Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

  8. 8. Rotate String

    8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...

  9. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

随机推荐

  1. Dev-cpp怎样去掉括号匹配?

    很多编C/C++的同学在用Dev-cpp的时候,就感觉到括号匹配很烦,又不知道哪里去掉. 所以,让ljn告诉你怎样去掉括号匹配. 1.打开Dev-cpp. 2.在菜单栏上,点击“工具[T]”,选择“编 ...

  2. java-Object类的解析(持续更新)

    1.getClass()方法 public class Object { /*一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用*/ private static native v ...

  3. Mysql InnoDB三大特性-- double write

    转自:http://www.ywnds.com/?p=8334 一.经典Partial page write问题? 介绍double write之前我们有必要了解partial page write( ...

  4. maven 打包zip,jsw相关

    参考链接: https://blog.csdn.net/masson32/article/details/51802732

  5. jquery 操作table样式拖动参考

    参考: http://blog.csdn.net/kdiller/article/details/6059727 http://www.jb51.net/article/59795.htm

  6. javascript进阶笔记(2)

    js是一门函数式语言,因为js的强大威力依赖于是否将其作为函数式语言进行使用.在js中,我们通常要大量使用函数式编程风格.函数式编程专注于:少而精.通常无副作用.将函数作为程序代码的基础构件块. 在函 ...

  7. bind配置文件

    options{} - 整个bind使用的全局配置选项 bind监听的端口,数据文件存储位置,缓存的存储位置,权限加密的控制 logging{}- 服务日志选项 日志输出的位置,以及输出的级别 zon ...

  8. js call回调的this指向问题

    function fn1(){ console.log(1); } function fn2(){ console.log(2); } fn1.call(fn2); //输出 1 fn1.call.c ...

  9. hibernate查询oracle数据库表报错SQL state [null]; error code [17027]; 流已被关闭; nested exception is java.sql.SQLException: 流已被关闭

    把表字段类型 long 修改为 number类型即可

  10. nio的简单学习

    参考链接: http://www.iteye.com/magazines/132-Java-NIO https://www.cnblogs.com/xiaoxi/p/6576588.html http ...