搜索字谜时,是否可以保持单词的大写和小写?

问题描述 投票:0回答:1

我需要编写一个程序,将整个文本文件读入字符串并在其中搜索字谜。输出必须是同一行中所有相同类型的字谜,且其原始大小写相同。

我尝试了以下操作,但没有得到想要的结果(显然都是小写):

String input = inputStringBuilder.toString();
input = input.replaceAll("[^äÄöÖüÜßa-zA-Z ]", "").toLowerCase();
String[] sentence = input.split(" ");

Map<String, Set<String>> anagrams = new HashMap<>();

for(int i = 0; i < sentence.length; i++){

        char[] charwords = sentence[i].toCharArray();

        Arrays.sort(charwords);

        String key = new String(charwords);

        Set<String> anagramSet = anagrams.get(key);
        if (anagramSet == null) {
          anagramSet = new HashSet<>();
          anagrams.put(key, anagramSet);
        }

   anagramSet.add(sentence[i]);

}
java anagram
1个回答
0
投票

首先您需要移动toLowerCase()呼叫。

input = input.replaceAll("[^äÄöÖüÜßa-zA-Z ]", ""); // <== Removed from here
String[] sentence = input.split(" ");

Map<String, Set<String>> anagrams = new HashMap<>();

for(int i = 0; i < sentence.length; i++){

        char[] charwords = sentence[i].toLowerCase().toCharArray(); // <== Added here

        Arrays.sort(charwords);

        String key = new String(charwords);

        Set<String> anagramSet = anagrams.get(key);
        if (anagramSet == null) {
          anagramSet = new HashSet<>();
          anagrams.put(key, anagramSet);
        }

   anagramSet.add(sentence[i]);

}

接下来,您需要从anagrams映射中消除不包含任何实际字谜的条目。

问题码中完全没有此步骤,其中Set大小为1的地图条目不是实际的字谜。

[现在Set包含具有其原始大小写的单词,假设没有real字谜,可能会出现像"The""the"这样的非字谜,并且也必须消除。如果有实字词,应保留各种大小写形式。

要检查这一点,请添加将所有单词都放入小写的集合中,并消除此新集合的大小是否为1,否则保留大小写集合。

// code from above here
for (Iterator<Set<String>> iter = anagrams.values().iterator(); iter.hasNext(); ) {
    Set<String> words = iter.next();
    if (words.size() == 1) {
        iter.remove(); // Not anagram: Single spelling only
    } else {
        Set<String> lower = new HashSet<>();
        for (String word : words)
            lower.add(word.toLowerCase());
        if (lower.size() == 1) {
            iter.remove(); // Not anagram: Multiple case variants, but all same spelling
        }
    }
}

Test

Input:  This is a test of 'the' and 'The'
Result: {}

Input:  This is a test of 'the', 'The', and 'eth'
Result: {eht=[the, The, eth]}

如果您不想保留同一单词的所有大小写变体,则只需使用new TreeSet<>(String.CASE_INSENSITIVE_ORDER) 使设置的大小写不敏感

(代码压缩,其中一些使用Java 8功能)

Map<String, Set<String>> anagrams = new HashMap<>();
for (String word : input.replaceAll("[^äÄöÖüÜßa-zA-Z ]", "").split(" ")) {
    char[] letters = word.toLowerCase().toCharArray();
    Arrays.sort(letters);
    String key = new String(letters);
    anagrams.computeIfAbsent(key, k -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER))
            .add(word);
}
anagrams.values().removeIf(words -> words.size() == 1);

Test

Input:  This is a test of 'the' and 'The'
Result: {}

Input:  This is a test of 'the', 'The', and 'eth'
Result: {eht=[eth, the]}

Input:  This is a test of 'The', 'the', and 'eth'
Result: {eht=[eth, The]}
© www.soinside.com 2019 - 2024. All rights reserved.