This is not same as "How to check if a string contains s specific substring?" . I found a no of questions like that here but not precisely what i am looking for. I am creating a program at a competitive coding site the problem of which states that we are given a string made of x,y,z and we have to count the number of substrings which contains atleast one of those chars but not all of them.I tried this...
String text = sc.next();
int l = text.length();
int count=0;
for(int j =1;j<=l;j++)
{
for(int i1 =0;i1<j;i1++){
String g = text.substring(i1,j);
if(g.contains("xyz")||g.contains("xzy")||g.contains("yzx")||g.contains("yxz")||g.contains("zxy")||g.contains("zyx"))
;
else
count++;
}
}
System.out.println(count);
And this worked(atleast for 2 test cases). But for the larger test cases my program is violating the time limit. Now i think that is because of the number of matching conditions in the if clause. I would like to know if there is any way by which i can just check if the substring contains 'xyz' in any order instead of checking for every order.Thanks! Any help is appreciated.
P.S- If anything else is responsible for the time limit violation, do mention out !