本文主要是介绍LeetCode459 Repeated Substring Pattern java solution,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目要求:
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;}
}
这篇关于LeetCode459 Repeated Substring Pattern java solution的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!