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.
Solutionclass 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; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/76811.html
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...
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...
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? 復(fù)雜度O(N^2),O(1) 代碼 public void ro...
摘要:?jiǎn)栴}描述解題思路使用數(shù)組自帶的方法和方法把數(shù)組最后一個(gè)取出來(lái)加入到頭部。使用數(shù)組的方法得到后個(gè)數(shù),再用方法刪去后個(gè)數(shù),最后用方法把得到的后個(gè)數(shù)添加到數(shù)組前面。 問(wèn)題描述: 189.Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, t...
摘要:題目詳情這道題目要求我們對(duì)一個(gè)正方形矩陣進(jìn)行順時(shí)針度的翻轉(zhuǎn)。并且要求不聲明額外的空間,不能新建二維數(shù)組。輸入數(shù)組旋轉(zhuǎn)后的輸入數(shù)組想法這道題因?yàn)橐笤谖?。所以我們需要找到一種解法,使得每次操作都是交換兩個(gè)元素的位置,最后實(shí)現(xiàn)整個(gè)矩陣的旋轉(zhuǎn)。 題目詳情 You are given an n x n 2D matrix representing an image.Rotate the ima...
閱讀 2603·2021-11-24 10:20
閱讀 2451·2021-09-10 10:51
閱讀 3433·2021-09-06 15:02
閱讀 3182·2019-08-30 15:55
閱讀 2905·2019-08-29 18:34
閱讀 3136·2019-08-29 12:14
閱讀 1278·2019-08-26 13:53
閱讀 2990·2019-08-26 13:43