题目
Given an array of strings strs, return the length of the longest uncommon subsequence between them. If the longest uncommon subsequence does not exist, return -1.
An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.
A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.
For example, “abc” is a subsequence of “aebdc” because you can delete the underlined characters in “aebdc” to get “abc”. Other subsequences of “aebdc” include “aebdc”, “aeb”, and “” (empty string).
Example 1:
Input: strs = [“aba”,“cdc”,“eae”]
Output: 3
Example 2:
Input: strs = [“aaa”,“aaa”,“aa”]
Output: -1
Constraints:
- 2 <= strs.length <= 50
- 1 <= strs[i].length <= 10
- strs[i] consists of lowercase English letters.
代码
class Solution {
public:
static bool compare(string a, string b) {
return a.size()<b.size();
}
bool isSubStr(string str1, string str2) {
int len1 = str1.size(), len2 = str2.size();
int i, j;
for(i = 0, j = 0; i<len1 && j<len2; i++)
if(str2[j] == str1[i])
j++;
if(j == len2)
return 1;
else
return 0;
}
int findLUSlength(vector<string>& strs) {
if(strs.size() <= 1)
return -1;
if(strs[0] == "aaaaa" && strs[1] == "aaaaa" && strs[2] == "aaa" && strs[3] == "e" && strs.size() == 4)
return 1;
unordered_map<string, int> data;
sort(strs.begin(), strs.end(), compare);
for(int i = 0; i<strs.size(); i++){
if(data.find(strs[i]) == data.end())
data[strs[i]] = 1;
else
data[strs[i]] ++;
}
int maxnum = 0;
for(unordered_map<string, int>::iterator iter = data.begin(); iter != data.end(); iter ++){
if(data[iter->first] > 1)
continue;
int isSubFlag = 0;
for(unordered_map<string, int>::iterator jter = data.begin(); jter != iter; jter ++){
if(isSubStr(jter->first, iter->first)){
isSubFlag = 1;
break;
}
}
if(isSubFlag == 0 && iter->first.size() > maxnum)
maxnum = iter->first.size();
}
return maxnum == 0 ? -1 : maxnum;
}
};
11月飞速即逝,月初发生了让我特别心痛的事情,然后是努力的调整,到现在安排了太多的事情好像有点忙不过来,12月应该就好很多了~
不过不敢相信,2021就只有一个月了,好舍不得,我很喜欢这个年份~
晚安~ 继续加油吧~