📌  相关文章
📜  包含所有给定单词的字符串的最短子字符串

📅  最后修改于: 2021-10-27 08:15:39             🧑  作者: Mango

打印包含所有给定单词的字符串的最短子字符串。

在第一个示例中,有两种可能的解决方案:“世界就在这里。这是一个充满“起起落落”和“起起落落”的生活。生活就是世界”。

  1. 使用所有需要搜索的给定单词初始化 HashMap 并将它们的值分配为 -1。
  2. 维护一个计数器。
  3. 逐字遍历整个字符串并对每个句子单词进行以下操作
    • 如果句子单词存在于您要查找的单词列表中,请更新该单词的最后位置。
    • 如果更新的最后一个位置未初始化,则增加总计数。
    • 如果总计数等于所有给定单词的计数,则循环遍历最后一个位置并找到最小的一个。当前位置和该值之间的距离将是子字符串的长度。记录这些值并在所有位置中找到最佳值

下面是上述步骤的Java实现。

// Java program to find the shortest substring of a
// string containing all given words using HashMap
import java.io.*;
import java.util.*;
import java.util.HashMap;
  
class Shortest {
  
    public static void findShortest(String sentence,
                                     String[] words)
    {
        // Make an array of words from given sentence
        // We remove punctuations before splitting.
        String replicate = sentence.replace(".", "");
        replicate = replicate.replace(", ", "");
        replicate = replicate.replace("!", "");
        String sent_words[] = replicate.split(" ");
  
        // hashmap to store given words in a map.
        HashMap map = new HashMap<>();
        int length = words.length;
        for (int i = 0; i < length; i++)
            map.put(words[i], -1);
  
        // Traverse through all sentence words
        // and if they match with given words
        // then mark their appearances in map.
        int len_sub = Integer.MAX_VALUE;
        int count = 0;
        int local_start = 0, local_end = 0;
        for (int i = 0; i < sent_words.length; i++) {
            if (map.containsKey(sent_words[i]) == true) {
  
                // If this is the first occurrence
                int index = map.get(sent_words[i]);
                if (index == -1)
                    count++;
  
                // Store latest index
                map.put(sent_words[i], i);
  
                // If all words matched
                if (count == length) {
  
                    // Find smallest index
                    int min = Integer.MAX_VALUE;
                    for (Map.Entry m :
                                     map.entrySet()) {
                        int val = m.getValue();
                        if (val < min)
                            min = val;
                    }
  
                    // Check if current length is smaller
                    // then length so far
                    int s = i - min;
                    if (s < len_sub) {
                        local_start = min;
                        local_end = i;
                        len_sub=s;
                    }
                }
            }
        }
  
        // Printing original substring (with punctuations)
        // using resultant local_start and local_end.
        String[] original_parts = sentence.split(" ");
        for (int i = local_start; i <=local_end; i++)
            System.out.print(original_parts[i] + " ");
    }
  
    // Driver code
    public static void main(String args[])
    {
        String sentence = "The world is here. this is a" +
            " life full of ups and downs. life is world.";
        String[] words = { "life", "ups", "is", "world" };
        findShortest(sentence, words);
    }
}

输出 :

ups and downs. life is 

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程