6

#yyds干货盘点# LeetCode程序员面试金典:字符串轮转

 1 year ago
source link: https://blog.51cto.com/u_13321676/5913094
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#yyds干货盘点# LeetCode程序员面试金典:字符串轮转

精选 原创

灰太狼_cxh 2022-12-05 16:58:09 博主文章分类:leetcode ©著作权

文章标签 字符串 编写代码 i++ 文章分类 Java 编程语言 阅读数180

字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。

输入:s1 = "waterbottle", s2 = "erbottlewat"

输出:True

输入:s1 = "aa", s2 = "aba"

输出:False

代码实现:

class Solution {
public boolean isFlipedString(String s1, String s2) {
int m = s1.length(), n = s2.length();
if (m != n) {
return false;
}
if (n == 0) {
return true;
}
for (int i = 0; i < n; i++) {
boolean flag = true;
for (int j = 0; j < n; j++) {
if (s1.charAt((i + j) % n) != s2.charAt(j)) {
flag = false;
break;
}
}
if (flag) {
return true;
}
}
return false;
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK