Java solution(28ms),easy to understand


  • 0
    A

    Just a simple idea that if s.substring(0, i) is the substring we looking for. slide whole string to position index i, the rest string must be absolutely matched.

    public class Solution {
        public boolean repeatedSubstringPattern(String s) {
            int len = s.length();
           for(int i = len / 2; i >= 1; i--){
               if(len % i == 0){
                   String sub = s.substring(i, len);
                   String pre = s.substring(0, len - i);
                   if(sub.equals(pre)) return true;
               }
           }
           return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.