/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package recyclednumbers;

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}

package com.edwarddrapkin;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Numbers {
	
	private boolean isRecylced(int m, int n) {
		String sm = m + "";
		String sn = n + "";
		
		//eliminate some common cases
		
		// same number apparently doesn't count...
		
		if(sm.equals(sn)) {
			return false;
		}
		
		//not the same length
		while(sm.length() < sn.length()) {
			sm = "0" + sm;
		}
		
		while(sn.length() < sm.length()) {
			sn = "0" + sn;
		}
		
		//chars in m not in n
		for(char c : sm.toCharArray()) {
			if(!sn.contains("" + c)) {
				return false;
			}
		}
		
		//vice versa
		for(char c : sn.toCharArray()) {
			if(!sm.contains("" + c)) {
				return false;
			}
		}
		
		for(int i = 1; i < sm.length(); i++) {
			String slice = sm.substring(i) + sm.substring(0, i);
			if(slice.equals(sn)) {
				return true;
			}
		}
		
		return false;
	}
	
	private static String[] readFile(String file) throws IOException {
		Scanner scanner = new Scanner(new File(file));
		
		String numLines = scanner.nextLine();
		
		int nl = Integer.parseInt(numLines);
		
		String[] lines = new String[nl];
		int i = 0;
		
		while(scanner.hasNextLine() && i < nl) {
			lines[i] = scanner.nextLine();
			++i;
		}
		
		return lines;
	}
	
	public static void main(String[] args) throws Exception {
		Numbers num = new Numbers();

		String[] lines = readFile("C:/Users/Eddie/Desktop/google_input.txt");
		
		int i = 1;
		for(String line : lines) {
			Scanner lScanner = new Scanner(line);
			int low = lScanner.nextInt();
			int high = lScanner.nextInt();
			
			int numPossible = 0;
			for(int idx = low; idx <= high; idx++) {
				for(int inner = idx; inner <= high; inner++) {
					if(num.isRecylced(idx, inner)) {
						//System.out.println(high + " " + low + " " + idx + " " + inner);
						numPossible++;
					}
				}
				
			}
			
			System.out.println("Case #" + i + ": " + numPossible);
			i++;
		}
	}
}













