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


public class RecycleNumber {
	
	int count=0;
	public RecycleNumber(int A,int B){
		for(int n=A;n<B-1;n++){
			for(int m=n+1;m<=B;m++){
				if(isRecycledPair(n,m)){
					count++;
				}
			}
		}
	}
	
	public int getCount() {
		return count;
	}


	boolean isRecycledPair(int a,int b){
		String sA=a+"";
		String sB=b+"";
		//start from 
		int countB=sB.length();
		int countA=sA.length();
		if(countA!=countB)return false;
		for(int i=0;i<sA.length();i++){
			if(sA.charAt(i)==sB.charAt(0)){
				if(sA.charAt(i)=='0') continue;
				if(compare(sA,i,sB))
					return true;
			}
		}

		return false;
	}
	private boolean compare(String sA, int i, String sB) {
		// TODO Auto-generated method stub
		int count=0;
		int length=sB.length();
		for(int j=i,k=0;count<length;k++,count++,j=(j+1)%length){
			if(sA.charAt(j)!=sB.charAt(k)){
				return false;
			}
		}
		return true;
	}

}
