import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
class AnagramSearch extends JFrame
{
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new AnagramSearch().setVisible(true);
}
});
}
private Set<String> words = null;
private JTextField input = null;
private JTextArea output = null;
public AnagramSearch()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
JPanel p = new JPanel(new GridLayout(1,3));
getContentPane().add(p, BorderLayout.NORTH);
JButton b = new JButton("Read and block GUI :-)");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
read("http://wortschatz.uni-leipzig.de/Papers/top10000de.txt");
}
});
p.add(b);
input = new JTextField("Angel");
p.add(input);
b = new JButton("Generate");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
generate();
}
});
p.add(b);
output = new JTextArea();
getContentPane().add(new JScrollPane(output));
setSize(600, 600);
}
private void read(String urlString)
{
words = new HashSet<String>();
try
{
URL url = new URL(urlString);
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while (true)
{
String s = br.readLine();
if (s == null)
{
break;
}
words.add(s.trim().toLowerCase());
}
}
catch (Exception e)
{
e.printStackTrace();
}
output.append("Read "+words.size()+" words\n");
}
private void generate()
{
String inputString = input.getText();
output.append("Anagrams of "+inputString+":\n");
inputString = inputString.trim().toLowerCase();
String inputArray[] = new String[inputString.length()];
for (int i=0; i<inputArray.length; i++)
{
inputArray[i] = ""+inputString.charAt(i);
}
PermutationIterable<String> pi = new PermutationIterable<String>(inputArray);
for (String s[] : pi)
{
String ss = "";
for (int i=0; i<s.length; i++)
{
ss += s[i];
}
if (words.contains(ss))
{
output.append(ss+"\n");
}
}
}
}
/**
* A class providing an iterator over all permutations of a set of
* elements. For a set S with n=|S|, there are m=n! different
* permutations. Example: <br />
* <pre>
* S = { A,B,C }, n = |S| = 3
* m = n! = 6
*
* Permutations:
* [A, B, C]
* [A, C, B]
* [B, A, C]
* [B, C, A]
* [C, A, B]
* [C, B, A]
* </pre>
*
* @author [url]http://www.java-forum.org/members/Marco13.html[/url]
*/
class PermutationIterable<T> implements Iterable<T[]>
{
private T input[];
private int numPermutations = 0;
public PermutationIterable(T... input)
{
this.input = input.clone();
numPermutations = factorial(input.length);
}
public Iterator<T[]> iterator()
{
return new Iterator<T[]>()
{
int current = 0;
public boolean hasNext()
{
return current < numPermutations;
}
// Adapted from [url=http://en.wikipedia.org/wiki/Permutation]Permutation - Wikipedia, the free encyclopedia[/url]
public T[] next()
{
T result[] = input.clone();
int factorial = numPermutations / input.length;
for (int i = 0; i < result.length - 1; i++)
{
int tempIndex = (current / factorial) %
(result.length - i);
T temp = result[i + tempIndex];
for (int j = i + tempIndex; j > i; j--)
{
result[j] = result[j - 1];
}
result[i] = temp;
factorial /= (result.length - (i + 1));
}
current++;
return result;
}
public void remove()
{
throw new UnsupportedOperationException(
"May not remove elements from a permutation");
}
};
}
/**
* Utility method for computing the factorial n! of a number n.
* The factorial of a number n is n*(n-1)*(n-2)*...*1, or more
* formally:<br />
* 0! = 1 <br />
* 1! = 1 <br />
* n! = n*(n-1)!<br />
*
* @param n The number of which the factorial should be computed
* @return The factorial, i.e. n!
*/
public static int factorial(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
{
f *= i;
}
return f;
}
}