Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using list of divisor
public static int geometricTrickv2(String s){
Set<Integer> indexA = new HashSet<>(s.length());
Set<Integer> indexC = new HashSet<>(s.length());
List<Integer> indexB = new ArrayList<>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='a')
indexA.add(i);
if(s.charAt(i)=='b')
indexB.add(i);
if(s.charAt(i)=='c')
indexC.add(i);
}
int res = 0;
for(int tmpB:indexB){
int powB = (int)Math.pow((double)(tmpB+1),2);
//Set<Integer> factors = new HashSet<>();
for(int i=2;i<=Math.sqrt((double)powB);i++){
if(powB%i==0){
if(i != powB/i)
if(indexA.contains(i-1)&&indexC.contains(powB/i-1))
res++;
if(indexC.contains(i-1)&&indexA.contains(powB/i-1))
res++;
else
if(indexA.contains(i-1)&&indexC.contains(i-1))
res++;
}
}
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.