796. Rotate String

Question

796. Rotate String

Solution

题目大意:两个字符串匹配

思路:Brute Force

Java实现:

public boolean rotateString(String A, String B) {
    if (A.length() != B.length()) return false;
    if (A.length() == 0) return true;
    // Brute Force
    for (int i=0; i<A.length(); i++) {
        int offset = i;
        boolean pass = true;
        for (int j = 0; j<B.length(); j++) {
            if (A.charAt((j + offset) % A.length()) != B.charAt(j)) {
                pass = false;
                break;
            }
        }
        if (pass) return true;
    }
    return false;
}
原文地址:https://www.cnblogs.com/okokabcd/p/9435879.html