How to find if a string S is contained inside a string made of S inserted at any position in S (only once) itself
<p>As a first check, since a valid input must be made from the insertion of the string into itself, it must be of size twice the string S.</p>

<p>Eg. If S=abc then ababca or aabcbc should return True but False for input such as abcab, abcxa, abcabcabc.  </p>

<p>I have already attempted the naive way of check the substring, if it exists then cut out that part and check if the remaining string matches S. But this fails for some type of inputs.</p>

<pre><code>private static void printResult(String s, String p){
    int x = p.indexOf(s);

    if(x&lt;0){
        System.out.println("False");
        return;
    }
    String s1="";
    if(p.length()&gt;=s.length()*2){
        s1 = p.substring(0,x)+p.substring(x+s.length());
        if(s1.equals(s)){
            System.out.println("True");
        }
        else{
            System.out.println("False");
        }
        return;
    }
    System.out.println("False");
}
</code></pre>