/*
 * 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 net.nickball.gcj.qual.recyclednumbers;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author nick
 */
public class RecycledNumbers {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BufferedReader reader = null;
        try {
            if(args.length != 2) 
                throw new IllegalArgumentException("Usage: RecycledNumbers <inputfile> <outputfile>");
            File inputFile = new File(args[0]);
            if (! inputFile.exists()) throw new FileNotFoundException("File not found! File: "+ args[0]);
            
            reader = new BufferedReader(new FileReader(inputFile));
            String cases = reader.readLine();
            
            String line;
            int caseCounter = 1; 
            while((line = reader.readLine()) != null) {
                String[] parts = line.split(" ");
                int A = Integer.parseInt(parts[0]);
                int B = Integer.parseInt(parts[1]);
                System.out.println("Case #" + caseCounter + ": " + getRecycledPairs(A,B));
                caseCounter++;
            }
            
        } catch(Exception e) {
            e.printStackTrace();
            System.exit(255);
        } finally {
            try {
                if(reader != null) reader.close();
            } catch(IOException e) {
                e.printStackTrace();
            } 
        }
    }

    private static long getRecycledPairs(int A, int B) {
        long count = 0;
        Set<String> uniques = new HashSet<String>(); 
        for(int n = A; n <=B; n++) {
            String nStr = Integer.toString(n);
            
            for(int m = n+1; m <=B; m++) {
                String mStr = Integer.toString(m);
                if(mStr.length() > nStr.length()) break;
                
                for(int index = 1; index < nStr.length(); index++) {
                    String start = new StringBuffer(nStr.substring(0,index)).reverse().toString();
                    String end = new StringBuffer(nStr.substring(index)).reverse().toString();
                    String rotated = StringUtils.stripStart(new StringBuffer(start).append(end).reverse().toString(),"0");
                    
                    //System.out.println(start +"," + end + "," + rotated);
                    if(rotated.equals(mStr) && (! uniques.contains(n + "" + m))) {
                        //System.out.println(n + "," + m);
                        uniques.add(n + "" + m);
                        count++;
                    }
                }
                
            }
        }
        return count;
    }
}
