Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Saturday, June 17, 2017

String formation from substring geeks solution


Problem : Check Whether a string is a formation of its repeated substring
Ex: aaaa
ababab


Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   String str=ab.next();
   String str2=str+str;
   str2=str2.substring(1,str2.length()-1);
   if(str2.contains(str))
   System.out.println("True");
   else
   System.out.println("False");
}
}
}

0 Comments:

Post a Comment

Stats