Anagrams : Algorithms « Collections Data Structure « Java






Anagrams

Anagrams
 

import java.io.IOException;

public class AnagramApp {
  static int size;

  static int count;

  static char[] charArray;

  public static void main(String[] args) throws IOException {
    String input = "Java Source and Support";
    size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
      charArray[j] = input.charAt(j);
    doAnagram(size);
  }

  public static void doAnagram(int newSize) {
    int limit;
    if (newSize == 1) // if too small, return;
      return;
    // for each position,
    for (int i = 0; i < newSize; i++) {
      doAnagram(newSize - 1); // anagram remaining
      if (newSize == 2) // if innermost,
        display(); 
      rotate(newSize); // rotate word
    }
  }

  // rotate left all chars from position to end
  public static void rotate(int newSize) {
    int i;
    int position = size - newSize;
    // save first letter
    char temp = charArray[position];
    //shift others left
    for (i = position + 1; i < size; i++)
      charArray[i - 1] = charArray[i];
    //put first on right
    charArray[i - 1] = temp;
  }

  public static void display() {
    System.out.print(++count + " ");
    for (int i = 0; i < size; i++)
      System.out.print(charArray[i]);
    System.out.println();
  }
}

           
         
  








Related examples in the same category

1.Hanoi puzzleHanoi puzzle
2.FibonacciFibonacci
3.Sieve Sieve
4.Find connections using a depth-first searchFind connections using a depth-first search
5.Find connections using hill climbing.
6.Find optimal solution using least-cost
7.Find the lost keysFind the lost keys
8.Compute the area of a triangle using Heron's FormulaCompute the area of a triangle using Heron's Formula
9.Compute prime numbers
10.Print a table of fahrenheit and celsius temperatures 1
11.Print a table of fahrenheit and celsius temperatures 2
12.Print a table of Fahrenheit and Celsius temperatures 3Print a table of Fahrenheit and Celsius temperatures 3
13.Soundex - the Soundex Algorithm, as described by KnuthSoundex - the Soundex Algorithm, as described by Knuth
14.A programmable Finite State Machine implementation.
15.An extendable Graph datastructure.
16.Utilities for flop (floating-point operation) counting.
17.LU Decomposition
18.Reverse Polish Notation
19.Permutator test
20.implements the LZF lossless data compression algorithm
21.Linear Interpolation
22.Utility class for generating the k-subsets of the numbers 0 to n
23.VersionVersion