E
E
evg_962017-10-23 15:07:36
Java
evg_96, 2017-10-23 15:07:36

How to find all anagrams in a word?

I read the book "Algorithms and Java Data Structures". Reached the topic "recursion". This chapter provides an example of a program for finding anagrams from a word.
But the problem is that the description of this algorithm is very poor. I can't figure this out on my own. Please tell me the link to the material where this algorithm for finding anagrams is explained or describe in your own words. Specifically, it is not clear why the word is displayed only when newSize == 2, and the work of the cyclic shift (the rotate() method) is not clear.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class App {
    static int size;
    static int count;
    static char[] arr = new char[100];

    public static void main(String[] args) throws IOException {
        System.out.print("Enter a word: ");

        String input = getString();

        size = input.length();
        count = 0;

        for (int i = 0; i < size; i++) {
            arr[i] = input.charAt(i);
        }

        doAnagram(size);
    }

    public static void doAnagram(int newSize) {
        if (newSize == 1) {
            return;
        }

        for (int i = 0; i < newSize; i++) {
            doAnagram(newSize - 1);

            if (newSize == 2) {
                displayWord();
            }

            rotate(newSize);
        }
    }

    public static void rotate(int newSize) {
        int i;
        int position = size - newSize;
        char temp = arr[position];

        for (i = position + 1; i < size; i++) {
            arr[i - 1] = arr[i];
        }

        arr[i - 1] = temp;
    }

    public static void displayWord() {
        if (count < 99) {
            System.out.print(" ");
        }

        if (count < 9) {
            System.out.print(" ");
        }

        System.out.print(++count + " ");

        for (int i = 0; i < size; i++) {
            System.out.print(arr[i]);
        }

        System.out.print("   ");
        System.out.flush();

        if (count % 6 == 0) {
            System.out.println("");
        }
    }

    public static String getString() throws IOException {
        InputStreamReader stream = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(stream);

        return in.readLine();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2017-10-23
@red-barbarian

These are just permutations.
rotate rotates the last n elements.
For example. if n = size, then 0 will be the last element, 1->0, 2->1, etc.
if n=size-m, size-m-> last, size-m+1->size-m, ...
it is clear that nothing is done for n = 1.
doAnagram.
with doAnagram(1) - return from recursion as it does nothing.
with doAnagram(n) - we cycle through getting n cases arr - each starts with a new letter arr[i] , i =0..n-1
anagrams for these cases will be arr[i] + doAnagram(n-1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question