Created
May 23, 2017 14:22
-
-
Save JulienRouse/cafbce417bbc2a4f6303df10df20d445 to your computer and use it in GitHub Desktop.
Using list of divisor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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