LeetCode459 Repeated Substring Pattern java solution

来源:互联网 发布:mac桌面下方软件 编辑:程序博客网 时间:2024/05/16 10:38

题目要求:

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: "abab"Output: TrueExplanation: It's the substring "ab" twice.

Example 2:

Input: "aba"Output: False

Example 3:

Input: "abcabcabcabc"Output: TrueExplanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

没有bug free

主要的原因:思路错误

public class Fix {    public boolean repeatedSubstringPattern(String str) {        int sumStr = str.length();        for(int i = sumStr / 2; i >= 1; i--) {            if(sumStr % i == 0) {                int num = sumStr / i;                StringBuffer sb = new StringBuffer();                String strx = str.substring(0, i);                for(int j = 0; j < num; j++) {                    sb.append(strx);                }                if(sb.toString().equals(str)) return true;            }        }        return false;    }}


1 0
原创粉丝点击