资讯专栏INFORMATION COLUMN

[LeetCode] Rotate String

MonoLog / 940人阅读

Problem

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.

Solution
class Solution {
    public boolean rotateString(String A, String B) {
        // if (A == null) return B == null;
        // if (B == null) return A == null;
        if (A.length() == 0) return B.length() == 0;
        if (A.length() != B.length()) return false;
        String AA = new String(A+A);
        for (int i = 0; i < A.length(); i++) {
            if (AA.substring(i, i+A.length()).equals(B)) return true;
        }
        return false;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76811.html

相关文章

  • [LintCode/LeetCode] Rotate Array

    Problem Given an array, rotate the array to the right by k steps, where k is non-negative. Example Example 1: Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the r...

    chanthuang 评论0 收藏0
  • [LeetCode] 48. Rotate Image

    Problem You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D mat...

    Warren 评论0 收藏0
  • LeetCode[48] Rotate Image

    LeetCode[48] Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 复杂度O(N^2),O(1) 代码 public void ro...

    sanyang 评论0 收藏0
  • LeetCode 189.Rotate Array

    摘要:问题描述解题思路使用数组自带的方法和方法把数组最后一个取出来加入到头部。使用数组的方法得到后个数,再用方法删去后个数,最后用方法把得到的后个数添加到数组前面。 问题描述: 189.Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, t...

    leanote 评论0 收藏0
  • leetcode 48 Rotate Image

    摘要:题目详情这道题目要求我们对一个正方形矩阵进行顺时针度的翻转。并且要求不声明额外的空间,不能新建二维数组。输入数组旋转后的输入数组想法这道题因为要求在位。所以我们需要找到一种解法,使得每次操作都是交换两个元素的位置,最后实现整个矩阵的旋转。 题目详情 You are given an n x n 2D matrix representing an image.Rotate the ima...

    kgbook 评论0 收藏0

发表评论

0条评论

MonoLog

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<