LeetCode 3076. Shortest Uncommon Substring in an Array

原题链接在这里:https://leetcode.com/problems/shortest-uncommon-substring-in-an-array/description/

题目:

You are given an array arr of size n consisting of non-empty strings.

Find a string array answer of size n such that:

  • answer[i] is the shortest 
    substring
     
     of arr[i] that does not occur as a substring in any other string in arr. If multiple such substrings exist, answer[i] should be the 
    lexicographically smallest
     
    . And if no such substring exists, answer[i] should be an empty string.

Return the array answer.

Example 1:

Input: arr = ["cab","ad","bad","c"]
Output: ["ab","","ba",""]
Explanation: We have the following:
- For the string "cab", the shortest substring that does not occur in any other string is either "ca" or "ab", we choose the lexicographically smaller substring, which is "ab".
- For the string "ad", there is no substring that does not occur in any other string.
- For the string "bad", the shortest substring that does not occur in any other string is "ba".
- For the string "c", there is no substring that does not occur in any other string.

Example 2:

Input: arr = ["abc","bcd","abcd"]
Output: ["","","abcd"]
Explanation: We have the following:
- For the string "abc", there is no substring that does not occur in any other string.
- For the string "bcd", there is no substring that does not occur in any other string.
- For the string "abcd", the shortest substring that does not occur in any other string is "abcd".

Constraints:

  • n == arr.length
  • 2 <= n <= 100
  • 1 <= arr[i].length <= 20
  • arr[i] consists only of lowercase English letters.

题解:

For each string, check all of its substring and see it appears in other strings.

Thus, we need to a structure to maintain all the substrings in all the strings. We can use a map.

Map key is the substring, value is frequeny on how many different string it appears.

To construct, we need to get all the substrings for each string, put them into a set and eventually accumlate it to the map.

Using set is beacuse if "aba", "a" substring will need to dedup first, otherwise, it could accumlate twice to the map.

Now, we have the map, then for each string, we check for its substrings, if its frequency is 1, it means it only appears in this string, not any other strings. It would be a good candidate.

If no candicates, add "" for res[k]. Otherwise, sort and take the first candidate.

Time Complexity: O(n * l ^ 3). n = arr.length. l is the average length of strings in arr. substring takes O(l) as well.

Space: O(n * l ^ 2). For string with length l, it could have O(l ^ 2) different substrings.

AC Java:

复制代码
 1 class Solution {
 2     public String[] shortestSubstrings(String[] arr) {
 3         int n = arr.length;
 4         HashMap<String, Integer> hm = new HashMap<>();
 5         for(String s : arr){
 6             HashSet<String> hs = new HashSet<>();
 7             for(int i = 0; i < s.length(); i++){
 8                 for(int j = i + 1; j <= s.length(); j++){
 9                     String sub = s.substring(i, j);
10                     hs.add(sub);
11                 }
12             }
13 
14             for(String can : hs){
15                 hm.put(can, hm.getOrDefault(can, 0) + 1);
16             }
17         }
18 
19         String[] res = new String[n];
20         for(int k = 0; k < n; k++){
21             String s = arr[k];
22             List<String> cans = new ArrayList<>();
23             for(int i = 0; i < s.length(); i++){
24                 for(int j = i + 1; j <= s.length(); j++){
25                     String sub = s.substring(i, j);
26                     if(hm.get(sub) == 1){
27                         cans.add(sub);
28                         break;
29                     }
30                 }
31             }
32 
33             if(cans.size() == 0){
34                 res[k] = "";
35             }else{
36                 Collections.sort(cans, (a, b) -> a.length() == b.length() ? a.compareTo(b) : a.length() - b.length());
37                 res[k] = cans.get(0);
38             }
39         }
40 
41         return res;
42     }
43 }
复制代码

 

posted @   Dylan_Java_NYC  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2022-08-04 LeetCode 1400. Construct K Palindrome Strings
2022-08-04 LeetCode 1950. Maximum of Minimum Values in All Subarrays
2022-08-04 LeetCode 2187. Minimum Time to Complete Trips
2022-08-04 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
2019-08-04 LeetCode 841. Keys and Rooms
2019-08-04 LeetCode 1061. Lexicographically Smallest Equivalent String
2019-08-04 LeetCode 1102. Path With Maximum Minimum Value
点击右上角即可分享
微信分享提示