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

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

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;
    }
}
elai30 回答:您如何重新排列字符以使单词存在于字典中?

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

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

以下是该方法的示例:

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","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: []

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

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,"");
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}
本文链接:https://www.f2er.com/3115352.html

大家都在问