0

So I am trying to make an anagram tool in java in which you insert a word/string, and it spits an anagram for that word. There probably easier or better ways to do this than I am about to show, but I am still curious. Here's what I wanted to do:

Lets say the Word is: apple

What I wanted to do is assign each character from that string a randomInt(100). So lets say as an example

a - 35, p - 54, p - 98, l - 75, e - 13

After that, I would want my program to sort the numbers from least to greatest, and then print the "new" string with the number's assigned character,least to greatest. In My case, the anagram would be: eaplp

All said and done, the place where I am stuck at is in how I can actually assign a character a random number from a string array, without actually changing that character into that number, and then printing that new modified string out like the way I said on top. Pseudocode or real code would be great.

Thanks

2
  • 1
    Use some kind of map. Commented Aug 5, 2015 at 2:38
  • An alternative implementation might be to place each character from the string into an array, randomly sort the array (i.e. using a Knuth shuffle), and then print each character in the array. Commented Aug 5, 2015 at 2:49

2 Answers 2

6

Use a TreeMap<Integer, Character>. Basic idea as follows:

TreeMap<Integer, Character> myMap = new TreeMap<Integer, Character>();
for (int i = 0; i < myString.length(); i++) {
  myMap.put((int)(Math.random() * 100), myString.charAt(i));
}

for (Map.Entry<Integer, Character> entry : myMap.entrySet()) {
  System.out.print(entry.getValue());
}
System.out.println();

A TreeMap automatically sorts the entries by key; thus, you don't have to perform a separate sort.


An easier way to code anagrams, though, is to convert the string to a character list, then use Collections.shuffle(). Basic idea:

List<Character> myLst = new ArrayList<Character>(myString.toCharArray());
Collections.shuffle(myLst);
for (Character c : myLst)
  System.out.print(c);
System.out.println();

There may be some compile errors in the above; I wrote it without checking, but the process should work.

Sign up to request clarification or add additional context in comments.

Comments

2

If you are using Java 8 a straightforward solution is a shuffled list of indices:

String word = "apple";
List<Integer> indices = IntStream.range(0, word.length()).collect(Collections.toList());
Collections.shuffle(indices);
indices.stream().mapToObj(word::charAt).forEach(System.out::print);

This can be done via an intermediate Map but it's a bit awkward and harder to follow:

Random random = new Random();
Map<Integer, Char> map = new TreeMap<>();
IntStream.range(0, word.length()).forEach(c -> map.put(random.nextInt(), c));
map.entrySet().stream().map(Map.Entry::getValue).forEach(System.out::print);

Or you can put it all in a single (hard to read) stream operation:

word.chars().boxed().collect(Collectors.toMap(random::nextInt, Function.identity()))
    .entrySet().stream().sorted(Map.Entry.comparingByKey())
    .map(e -> Character.toChars(e.getValue()))
    .forEach(System.out::print);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.