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: True Explanation: It's the substring "ab" twice. Example 2: Input: "aba" Output: False Example 3: Input: "abcabcabcabc" Output: True Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.) Input: "bb" Output: false Expected: true // passed 117/120 cases class Solution { public boolean repeatedSubstringPattern(String s) { // 2 parts , 3 parts, ... at most s.length / 2 parts // get the substring , append it n times , check if same as string s // string.equals(), char use == for(int i = 2; i <= s.length() / 2; i++){ if(s.length() % i == 0){ String sub = s.substring(0, s.length() / i); StringBuilder sb = new StringBuilder(); for(int j = 0; j < i; j++){ sb.append(sub); } if(sb.toString().equals(s)) return true; } } return false; } } // correct , covered all the cases class Solution { public boolean repeatedSubstringPattern(String s) { // 2 parts , 3 parts, ... at most s.length / 2 parts // get the substring , append it n times , check if same as string s // string.equals(), char use == int l = s.length(); for(int i = l / 2; i >= 1; i--){ // the length of each part if(l % i == 0){ // if the part is divisible int m = l / i; // how many parts String substring = s.substring(0, i); StringBuilder sb = new StringBuilder(); for(int j = 0; j < m; j++){ sb.append(substring); } if(sb.toString().equals(s)) return true; } } return false; } }