Using Regex Pattern To Solve The Problem


  • 0
    A
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    

    public class Solution {

    public boolean repeatedSubstringPattern(String s) {
        //Input: "abcabcabcabc"
        //Output: True
        Pattern r = Pattern.compile("^(\\w+)\\1+$");
        Matcher m = r.matcher(s);
        try{
        m.find();
        String target=m.group(1);
        System.out.println("target is" + target);
        if(target!=null && s.contains(target))
            return true;
        return false;
        }catch (IllegalStateException ex){
            return false;
        }
      
        
    }
    

    }


Log in to reply
 

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