Project Euler – Problem 62

Problem 62:
The cube, 41063625 (345^3), can be permuted to produce two other cubes: 56623104 (384^3) and 66430125 (405^3).

In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.

Find the smallest cube for which exactly five permutations of its digits are cube.

package runner;
 
import java.math.BigInteger;
import java.util.HashSet;
import java.util.List;
import java.util.Vector;
 
class runner
{
	private static void setup(HashSet<List<BigInteger>> store, int max, int limit){
		int len = 1;
		List<BigInteger> storeDigits = new Vector<BigInteger>(20);
		for(int i=1;i<limit;i++){
			BigInteger current = new BigInteger(""+i); current = current.pow(3);
			if(len < current.toString().length()){
				if(storeDigits.size() >= max) store.add(storeDigits);
				len = current.toString().length();
				storeDigits = new Vector<BigInteger>(10);
			}
			storeDigits.add(current);
		}
		if(storeDigits.size() >= max) store.add(storeDigits);
	}
	public static boolean isPermutationOf(String strI, String strJ){
		int[] count = new int[48+10];
		for(int i=0;i<strI.length();i++){
			count[strI.charAt(i)]++;
		}
		for(int i=0;i<strJ.length();i++){
			count[strJ.charAt(i)]--;
			if(count[strJ.charAt(i)] < 0) return false;
		}
		for(int i=48; i<48+10; i++){
			if(count[i] != 0) return false;
		}
		return true;
	}
	public static void main (String[] args) throws java.lang.Exception
	{
		long time = System.currentTimeMillis();
 
		HashSet<List<BigInteger>> store = new HashSet<List<BigInteger>>(10);
		setup(store, 5, 10000);
 
		outerLoop:
		for(List<BigInteger> set : store){
			boolean[] tried = new boolean[set.size()];
			for(int i=0;i<set.size()-5;i++){
				String str_i = set.get(i).toString();
 
				tried[i] = true;
				int c = 1;
				for(int j=i+1; j<set.size();j++){
					if(tried[j]) continue;
 
					if(isPermutationOf(str_i, set.get(j).toString())){
						tried[j] = true;
						c++;
					}
				}
				if(c == 5){
					System.out.println(str_i);
					break outerLoop;
				}
			}
		}
 
		System.out.println("time:"+(System.currentTimeMillis()-time));
	}
}