Java One Line Solution Using strStr() (Problem 28)


  • 0
    O

    public class Solution {
    public boolean repeatedSubstringPattern(String s) {
    return findStr((s+s).substring(1,2*s.length()-1),s);
    }
    public boolean findStr(String haystack, String needle) {
    if(needle.length() == 0) return true;
    for(int i = 0; i < haystack.length(); i++)
    if(i+needle.length() <= haystack.length() && haystack.charAt(i) == needle.charAt(0))
    if(haystack.substring(i, i+needle.length()).equals(needle))
    return true;
    return false;
    }
    }


Log in to reply
 

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