美文网首页
Rotate String

Rotate String

作者: BLUE_fdf9 | 来源:发表于2018-10-18 10:12 被阅读0次

    题目
    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
    before rotate: s: "abcde"
    after rotate: cdeab (right substring of s + left substring of s)

    If we concatenate s twice, i.e: ss = s + s = abcdeabcde
    the string after rotation will show up in ss

    class Solution {
        public boolean rotateString(String A, String B) {
            String AA = A + A;
            return A.length() == B.length() && AA.contains(B);
        }
    }
    

    相关文章

      网友评论

          本文标题:Rotate String

          本文链接:https://www.haomeiwen.com/subject/uerjzftx.html