/*
 * 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;
    }
}

import java.awt.datatransfer.*;
import java.awt.Toolkit;
public class Repeat
{
    private static String inp;
    public Repeat()
    {
        
    }

    public static void main(String[] args)
    {
        Transferable temp = (Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null));
        try {
            inp = (String) temp.getTransferData(DataFlavor.stringFlavor);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        System.out.println(inp);
        
        System.out.println("Breakdown >" + inp.substring(inp.indexOf("\n")));
        //int iterations = Integer.parseInt(inp.substring(inp.indexOf("\n")));
        String[] newlineSplit = inp.split("\n");
        int iterations = Integer.parseInt(newlineSplit[0]);
        System.out.println(iterations);
        System.out.println("Solutions:\n\n\n\n");
        //String cases = inp.substring(inp.indexOf("\n"));
        for(int i = 1; i < newlineSplit.length;i++)
        {
            String[] nums = newlineSplit[i].split(" ");
            
           getCount(Integer.parseInt(nums[0]) ,Integer.parseInt(nums[1]), i);
        }
    }
    
    public static void getCount(int a, int b, int caseNum)
    {
        int count = 0;
        while(a <= b)
        {
            String sA = "" + a;
            String sB = "" + b;
            //System.out.println(sA + "   " + sB);
            for(int i = 0; i < sA.length();i++)
            {
                String temp = sA.substring(sA.length() - i-1) + sA.substring(0,sA.length() - i - 1);
                int iT = Integer.parseInt(temp);
                int iA = Integer.parseInt(sA);
                
                //System.out.println(">>[" + temp + "]    " + (iT - iA) );
                if(iT - iA > 0 && iT <= b)
                {
                    count++;
                    //System.out.println(">>>>>>> " + sA + " " + temp + "    " + (iT - iA));
                } 
                //else
                    //System.out.println(">>[" + temp + "]    " + (iT - iA) );
            }
            a++;
        }
        System.out.println("Case #" + caseNum + ": " + count);
        
    }
}
