/*
 * 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.google.codjam.problems;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class RecycledNumbers implements ProblemInterface {
    
    
    public String run(ArrayList dataCase) {
        ArrayList numbers= dataCase;
        String aString = numbers.get(0).toString().split(" ")[0];
        String bString = numbers.get(0).toString().split(" ")[1];
        HashMap map = new HashMap();
        
        int A = Integer.parseInt(aString) ;
        int B = Integer.parseInt( bString) ;
       
        int counter =0;
        int minMov=1;
        int currentM=minMov;
        int maxMov=aString.length();
        System.out.println("***************INICIO");
            while(maxMov>currentM)
            {
                for ( int i=A ; i <B+1 ; i++ )  
                {
                    if(findM(i,A,B,currentM,maxMov,map))
                    {
                        counter++;
                    }
                }currentM++;
            }
        
        System.out.println("***************FIN");
        return counter+"";
    }
    
    public boolean findM(int n,int A,int B, int movs,int max,HashMap map)
    {
        String nS=n+"";
        String finalS="";
        String tempS="";
        String endS="";
        nS=n+"";
        tempS = nS.substring(0,max-movs);
        endS =nS.substring(max-movs,max);
        finalS= endS+tempS;
        int m =Integer.parseInt(finalS);
        if((m>A-1&&m<B+1)&&n<m)
        {
            System.out.println("( "+n+" , " +m+")");
            String key = n+""+m;
            if(!map.containsKey(key))
            {
                map.put(key,null);
                return true;
            }
        }
    
        
        return false;
    }
}
