796. Rotate String - LeetCode

Question

796. Rotate String java

Solution

題目大意:兩個字符串匹配code

思路:Brute Forceip

Java實現:leetcode

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;
}
相關文章
相關標籤/搜索