Skip to content

Latest commit

 

History

History
72 lines (52 loc) · 1.58 KB

README.md

File metadata and controls

72 lines (52 loc) · 1.58 KB

题目描述

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

示例1:

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

示例2:

 输入:s1 = "aa", "aba"
 输出:False

提示:

  1. 字符串长度在[0, 100000]范围内。

说明:

  1. 你能只调用一次检查子串的方法吗?

解法

Java

class Solution {
    public boolean isFlipedString(String s1, String s2) {
        int len1 = s1.length(), len2 = s2.length();
        if (len1 != len2) {
            return false;
        }
        if ((len1 == 0 && len2 == 0) || (s1.equals(s2))) {
            return true;
        }

        for (int i = 0; i < len1; ++i) {
            s1 = flip(s1);
            if (s1.equals(s2)) {
                return true;
            }
        }
        return false;

    }

    private String flip(String s) {
        return s.substring(1) + s.charAt(0);
    }
}

...