您如何重新排列字符以使字典中存在一个单词?

How can you rearrange characters to make a word existing in the dictionary?

我一直在尝试在这里和那里调整一些代码以使我的输出正确。我试图让我的代码能够重新排列单词中的字母,以生成 words.txt 中存在的其他单词,来自 https://github.com/dwyl/english-words。任何帮助,将不胜感激。谢谢

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class WordsInWords {
    public static void main(String[] args) throws IOException {
        String file = "/Users/laptop/Desktop/Test.txt";
        BufferedReader r = new BufferedReader(new FileReader(file));
        StringBuilder b = new StringBuilder();
        String c = r.readLine();
        while (c != null) {
            b.append(c);
            b.append(" ");
            c = r.readLine();
        }
        Scanner s = new Scanner(System.in);
        String in = s.nextLine();
        char[] input = new char[in.length()];
        for (int i = 0; i < input.length; i++) {
            input[i] = in.charAt(i);
        }
        char[] temp = null;
        for (int i = 0; i < b.length(); i++) {
            if (i < b.length() - 1 && b.charAt(i) == ' ' && b.charAt(i + 1) != ' ') {
                boolean found = false;
                int counter = 0;
                while (!found) {
                    counter++;
                    if (b.charAt(i + counter) == ' ') {
                        found = true;
                        temp = new char[counter - 1];
                        for (int j = i + 1; j < i + counter; j++) {
                            temp[j] = b.charAt(j);
                        }
                    }
                }
            }
        }
        if (Arrays.asList(input).contains(temp)) {
            System.out.println(temp);
        }
    }
}

这是我调整后的代码:

        import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WordsInWords {
    public static void main(String[] args) throws IOException {
        String file = "/Users/laptop/Desktop/words.txt";
        BufferedReader r = new BufferedReader(new FileReader(file));
        String[] words;
        String c = r.readLine();
        int a=0;
        while (c != null) {
            c = r.readLine();
            a++;
        }
        words=new String[a];
        a=0;
        r = new BufferedReader(new FileReader(file));
        String temp=r.readLine();
        while (temp != null) {
            words[a]=r.readLine();
            temp=words[a];
            a++;
        }
        for (int i = 0; i < words.length; i++) {
            System.out.println(words[i]);
        }
        Scanner s = new Scanner(System.in);
        String input = s.nextLine();
            List<String> found = findRearranged(input, words);
            System.out.println("For '" + input + "' found: " + Arrays.toString(found.toArray()));
    }

    public static List<String> findRearranged(String input, String[] words) {
        List<String> found = new ArrayList<>();
        for (String w : words) {
            if (hasSameLetters(w, input)) {
                found.add(w);
            }
        }
        return found;
    }

    public static boolean hasSameLetters(String a, String b) {
        if (a.length() != b.length()) {
            return false;
        }
        while (a.length() > 0) {
            for (char c : b.toCharArray()) {
                int index = a.indexOf(c);
                if (index >= 0) {
                    a = a.replace(String.valueOf(c), "");
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}

是否需要重新排列字符? 重新排列输入并通过字典搜索以找到相同的词可能会花费大量的计算时间,单个几个字母的词可以有很多排列。

对我来说,你似乎想在字典中查找包含相同字母的输入词的单词(换句话说,如果输入的词被重新排列,它会为你提供字典中现有的词)。可能检查两个单词是否具有完全相同的字母,无论它们在两个字符串中的位置是否满足要求。

这是该方法的示例:

public class Sample {
    public static void main(String[] args) {
    //the words in dictionary
        String[] words = {"words", "sword", "nord", "chord", "score", "cores", "mors", "xyz", "scores", "ordsw"};
        String[] input = {"sword", "score", "tores", "nores"};
        for (String i : input) {
            List<String> found = findRearranged(i, words);
            System.out.println("For '" + i + "' found: " + Arrays.toString(found.toArray()));
        }
    }

    public static List<String> findRearranged(String input, String[] words) {
        List<String> found = new ArrayList<>();
        for (String w : words) {
            if (hasSameLetters(w, input)) {
                found.add(w);
            }
        }
        return found;
    }

    public static boolean hasSameLetters(String a, String b) {
        if (a.length() != b.length()) {
            return false;
        }
        while (a.length() > 0) {
            for (char c : b.toCharArray()) {
                int index = a.indexOf(c);
                if (index >= 0) {
                    a = a.replace(String.valueOf(c), "");
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}

这输出在:

For 'sword' found: [words, sword, ordsw]
For 'score' found: [score, cores]
For 'tores' found: []
For 'nores' found: []

编辑: 我看到假设是每个词都在它自己的行中。 我看到您已经想到了计算文件中的单词数,但在这种情况下,最好使用具有动态大小的集合。 这是固定示例:

public class WordsInWords {
    public static void main(String[] args) throws IOException {
        String file = "C:\Users\masta\IdeaProjects\podstawka-spring-java\words.txt";
        BufferedReader r = new BufferedReader(new FileReader(file));
        List<String> words = new ArrayList<>();
        String c = r.readLine();
        while (c != null) {
            words.add(c);
            c = r.readLine();
        }
        for (int i = 0; i < words.size(); i++) {
            System.out.println("Words: " + words.get(i));
        }
        Scanner s = new Scanner(System.in);
        String input = s.nextLine();
        List<String> found = findRearranged(input, words);
        System.out.println("For '" + input + "' found: " + Arrays.toString(found.toArray()));
    }

    public static List<String> findRearranged(String input, List<String> words) {
        List<String> found = new ArrayList<>();
        for (String w : words) {
            if (hasSameLetters(w, input)) {
                found.add(w);
            }
        }
        return found;
    }

    public static boolean hasSameLetters(String a, String b) {
        if (a.length() != b.length()) {
            return false;
        }
        while (a.length() > 0) {
            for (char c : b.toCharArray()) {
                int index = a.indexOf(c);
                if (index >= 0) {
                    a = a.replace(String.valueOf(c), "");
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}