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 shortestarr[i]
that does not occur as a substring in any other string inarr
. If multiple such substrings exist,answer[i]
should be theanswer[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 }