/*
 * 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 test.googlejam.year2012.jam;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import test.googlejam.base.ProblemProcessor;
import test.googlejam.base.ValueReader;

public class RecycledNumbers extends ProblemProcessor
{

	static java.util.Map<Integer, Integer> mapping = new java.util.TreeMap<Integer, Integer>();
	public static void main(String[] args) throws java.io.IOException
	{
		RecycledNumbers processor;
		String fileName = "c:/temp/googlejam/RecycledNumbers";

		processor = new RecycledNumbers();
		processor.setInputFileName(fileName + args[0] + ".in");
		processor.setOutputFileName(fileName + args[0] + ".out");
		processor.start();
	}

	@Override
	public String processCase(int caseNumber) throws IOException
	{
		java.util.List<String> values;
		
		values = this.readValues(" ", new ValueReader<String>() {
			@Override
			public String readValue(String token) {
				return token;
			}
		});
		
		String stringA;
		int valueA, valueB;
		stringA = values.get(0);
		valueA = Integer.valueOf(values.get(0));
		valueB = Integer.valueOf(values.get(1));
		java.util.SortedSet<Integer> permutations = new java.util.TreeSet<Integer>();

		for ( int currentValue = valueA; currentValue <= valueB; currentValue++ )
		{
			String currentString = Integer.toString(currentValue);
			for ( int i = 1; i < currentString.length(); i++ )
			{
				String newString = StringUtils.right(currentString, i) + StringUtils.left(currentString, currentString.length() - i);
				if ( '0' == newString.charAt(0) ) continue;
				int newValue = Integer.valueOf(newString);
				if ( (valueA <= newValue) && (newValue <= valueB) )
					permutations.add(newValue);
			}
		}

		System.out.println(valueA);
		System.out.println(valueB);
		System.out.println(permutations);
		java.util.Set<String> foundCases = new java.util.HashSet<String>();
		for ( int currentValue : permutations )
		{
			String currentString = Integer.toString(currentValue);
			for ( int i = 1; i < currentString.length(); i++ )
			{
				String newString = StringUtils.right(currentString, i) + StringUtils.left(currentString, currentString.length() - i);
				if ( '0' == newString.charAt(0) ) continue;
				int newValue = Integer.valueOf(newString);
				if ( (currentValue < newValue) && (permutations.contains(newValue)))
				{
					String foundCase = valueA + " <= " + currentValue + " < " + newValue + " <= " + valueB;
					//System.out.println(foundCase);
					foundCases.add(foundCase);
				}
			}
		}
		//System.out.println(foundCases);
		return Integer.toString(foundCases.size());
	}
}
