Skip to content

Instantly share code, notes, and snippets.

@JulienRouse
Created May 23, 2017 14:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save JulienRouse/cafbce417bbc2a4f6303df10df20d445 to your computer and use it in GitHub Desktop.
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