We have to find number of substrings of a String that contain some anagram of another String as a subsequence.
The substrings are considered different only if there start or end positions differ.
String="aba"
anotherString="a"
Occurence of "a" in "aba" is as follows :
a at index 0..0
ab at index 0..1
aba at index 0..2
ba at index 1..2
a at index 2..2
i.e total of 5 times...so o/p=5
(the start and end points here, are inclusive)
I thought this question as one of the application of "number of occurrences of a subsequence in a string" and "Find the smallest window in a string containing all characters of another string".
But even after many changes in the combined code I am unable to come up with the solution . Pasting my code is of no use as I know where I am wrong .what I want to know is how can we solve this efficiently without brute force solution.
Code :
public static void findLengthAndSequence(String str1,String str2){
int begin=0,biginWith=0,endWith=0,count=0,minLen=Integer.MAX_VALUE,len=0;
int l=0;
int [] hasFound=new int[256];
int [] toFound=new int[256];
for(int i=0;i<str2.length();++i){
toFound[(int)str2.charAt(i)]++;
}
for(int end=0;end<str1.length();++end){
if(toFound[(int)str1.charAt(end)]==0)
continue;
hasFound[(int)str1.charAt(end)]++;
if(hasFound[(int)str1.charAt(end)]<=toFound[(int)str1.charAt(end)]){
count++;
}
if(count==str2.length()){
l++; //add this to find number of such anagram in string
System.out.println("l= "+l+" "+begin+" "+end);
while(toFound[(int)str1.charAt(begin)]==0 || hasFound[(int)str1.charAt(begin)]>toFound[(int)str1.charAt(begin)] )
{
if(hasFound[(int)str1.charAt(begin)]>toFound[(int)str1.charAt(begin)]){
hasFound[(int)str1.charAt(begin)]-=1;
}
begin++;
}//while
len=end-begin+1;
if(minLen>len){
minLen=len;
endWith=end;
biginWith=begin;
}
}//if
}//end
for(int i=biginWith;i<=endWith;++i){
System.out.print(""+str1.charAt(i));
}
}
This code gives output =3 to above question. I know I am not able to check every substring in this once I traversed remaining substrings once I reach end of first string.
e.g in "aba" my code checks for a,ab,aba.but once I reach the end it will not check
ba,a .since we need to count this also as they are having different index values.
Is there any way other than brute force of exponential time complexity to check for every possible substring..