## Monday, April 17, 2017

### LeetCode 522 - Longest Uncommon Subsequence II

https://leetcode.com/problems/longest-uncommon-subsequence-ii
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
Example 1:
```Input: "aba", "cdc", "eae"
Output: 3
```
Note:
1. All the given strings' lengths will not exceed 10.
2. The length of the given list will be in the range of [2, 50].
X.
http://code.bitjoy.net/2017/06/12/leetcode-longest-uncommon-subsequence-ii/

`    ``// 判断child是否是par的子序列`
`    ``bool` `isSubSeq(``const` `string& par, ``const` `string& child) {`
`        ``int` `i = 0, j = 0, m = par.size(), n = child.size();`
`        ``while` `(i < m&&j < n) {`
`            ``if` `(par[i] == child[j])++j;`
`            ``++i;`
`        ``}`
`        ``return` `j == n;`
`    ``}`
`public``:`
`    ``int` `findLUSlength(vector<string>& strs) {`
`        ``unordered_map<string, ``int``> hash;`
`        ``for` `(``const` `auto``& s : strs)++hash[s];`
`        ``sort(strs.begin(), strs.end(), [](``const` `string& s1, ``const` `string& s2) {``return` `s1.size() > s2.size(); });`
`        ``for` `(``int` `i = 0; i < strs.size(); ++i) {`
`            ``if` `(hash[strs[i]] > 1)``continue``;`
`            ``bool` `isSub = ``false``;`
`            ``for` `(``int` `j = i - 1; j >= 0; --j) {`
`                ``if` `(isSubSeq(strs[j], strs[i])) {`
`                    ``isSub = ``true``;`
`                    ``break``;`
`                ``}`
`            ``}`
`            ``if` `(!isSub)``return` `strs[i].size();`
`        ``}`
`        ``return` `-1;`
`    ``}`
`};`

```    int findLUSlength(vector<string>& strs) {
int n = strs.size();
unordered_set<string> s;
sort(strs.begin(), strs.end(), [](string a, string b){
if (a.size() == b.size()) return a > b;
return a.size() > b.size();
});
for (int i = 0; i < n; ++i) {
if (i == n - 1 || strs[i] != strs[i + 1]) {
bool found = true;
for (auto a : s) {
int j = 0;
for (char c : a) {
if (c == strs[i][j]) ++j;
if (j == strs[i].size()) break;
}
if (j == strs[i].size()) {
found = false;
break;
}
}
if (found) return strs[i].size();
}
s.insert(strs[i]);
}
return -1;
}```
https://discuss.leetcode.com/topic/85044/python-simple-explanation
When we add a letter Y to our candidate longest uncommon subsequence answer of X, it only makes it strictly harder to find a common subsequence. Thus our candidate longest uncommon subsequences will be chosen from the group of words itself.
Suppose we have some candidate X. We only need to check whether X is not a subsequence of any of the other words Y. To save some time, we could have quickly ruled out Y when len(Y) < len(X), either by adding "if len(w1) > len(w2): return False" or enumerating over A[:i] (and checking neighbors for equality.) However, the problem has such small input constraints that this is not required.
We want the max length of all candidates with the desired property, so we check candidates in descending order of length. When we find a suitable one, we know it must be the best global answer.
``````def subseq(w1, w2):
#True iff word1 is a subsequence of word2.
i = 0
for c in w2:
if i < len(w1) and w1[i] == c:
i += 1
return i == len(w1)

A.sort(key = len, reverse = True)
for i, word1 in enumerate(A):
if all(not subseq(word1, word2)
for j, word2 in enumerate(A) if i != j):
return len(word1)
return -1``````

``````public int findLUSlength(String[] strs) {
int len = strs.length;

// reverse sorting array with length
Arrays.sort(strs, new Comparator<String>() {
public int compare(String s1, String s2) {
return s2.length() - s1.length();
}
});

for(int i=0; i<len; i++){
int missMatchCount = strs.length - 1;
for(int j=0; j<len; j++){
if(i != j && !isSubSequence(strs[i], strs[j])){
missMatchCount --;
}
}

// strs[i] is not a sub sequence of any other entry
if(missMatchCount == 0){
return strs[i].length();
}
}

return -1;
}

private boolean isSubSequence(String s1, String s2){
int idx = 0;
for(char ch : s2.toCharArray()){
if(idx < s1.length() && ch == s1.charAt(idx)){
idx++;
}
}

return idx == s1.length();
}``````
X.
http://www.cnblogs.com/grandyang/p/6680548.html

```    int findLUSlength(vector<string>& strs) {
int res = -1, j = 0, n = strs.size();
for (int i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (i == j) continue;
if (checkSubs(strs[i], strs[j])) break;
}
if (j == n) res = max(res, (int)strs[i].size());
}
return res;
}
int checkSubs(string subs, string str) {
int i = 0;
for (char c : str) {
if (c == subs[i]) ++i;
if (i == subs.size()) break;
}
return i == subs.size();
}```

X.
https://discuss.leetcode.com/topic/85027/java-hashing-solution
We simply maintain a map of all subsequence frequencies and get the subsequence with frequency 1 that has longest length.
NOTE: This solution does not take advantage of the fact that the optimal length subsequence (if it exists) is always going to be the length of some string in the array. Thus, the time complexity of this solution is non-optimal. See https://discuss.leetcode.com/topic/85044/python-simple-explanation for optimal solution.
``````public int findLUSlength(String[] strs) {
Map<String, Integer> subseqFreq = new HashMap<>();
for (String s : strs)
for (String subSeq : getSubseqs(s))
subseqFreq.put(subSeq, subseqFreq.getOrDefault(subSeq, 0) + 1);
int longest = -1;
for (Map.Entry<String, Integer> entry : subseqFreq.entrySet())
if (entry.getValue() == 1) longest = Math.max(longest, entry.getKey().length());
return longest;
}

public static Set<String> getSubseqs(String s) {
Set<String> res = new HashSet<>();
if (s.length() == 0) {
return res;
}
Set<String> subRes = getSubseqs(s.substring(1));
for (String seq : subRes) res.add(s.charAt(0) + seq);
return res;
}``````