★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ylfcbcne-me.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.git
A 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.github
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:函数
给定字符串列表,你须要从它们中找出最长的特殊序列。最长特殊序列定义以下:该序列为某字符串独有的最长子序列(即不能是其余字符串的子序列)。this
子序列能够经过删去字符串中的某些字符实现,但不能改变剩余字符的相对顺序。空序列为全部字符串的子序列,任何字符串为其自身的子序列。spa
输入将是一个字符串列表,输出是最长特殊序列的长度。若是最长特殊序列不存在,返回 -1 。 code
示例:htm
输入: "aba", "cdc", "eae" 输出: 3
提示:
1 class Solution { 2 func findLUSlength(_ strs: [String]) -> Int { 3 var strs = strs 4 var n:Int = strs.count 5 var s:Set<String> = Set<String>() 6 strs.sort(by:{ 7 if $0.count == $1.count 8 { 9 return $0 > $1 10 } 11 return $0.count > $1.count 12 }) 13 for i in 0..<n 14 { 15 if i == n - 1 || strs[i] != strs[i + 1] 16 { 17 var found:Bool = true 18 for a in s 19 { 20 var j:Int = 0 21 for c in a.characters 22 { 23 if c == strs[i][j] {j += 1} 24 if j == strs[i].count {break} 25 } 26 if j == strs[i].count 27 { 28 found = false 29 break 30 } 31 } 32 if found {return strs[i].count} 33 } 34 s.insert(strs[i]) 35 } 36 return -1 37 } 38 } 39 40 extension String { 41 //subscript函数能够检索数组中的值 42 //直接按照索引方式截取指定索引的字符 43 subscript (_ i: Int) -> Character { 44 //读取字符 45 get {return self[index(startIndex, offsetBy: i)]} 46 } 47 }
16ms
1 class Solution { 2 func findLUSlength(_ strs: [String]) -> Int { 3 var sortStrs = strs.sorted { 4 return $1.count < $0.count || ($1.count == $0.count && $1 < $0 ) 5 } 6 let duplicates = getDuplicates(sortStrs) 7 for i in sortStrs.indices { 8 if !duplicates.contains(sortStrs[i]) { 9 if i == 0 { return sortStrs[i].count } 10 for j in 0..<i { 11 if isSubsequence(Array(sortStrs[j]), Array(sortStrs[i])) { 12 break 13 } 14 if j == i-1 { 15 return sortStrs[i].count 16 } 17 } 18 } 19 } 20 return -1 21 } 22 23 func isSubsequence(_ chars1: [Character], _ chars2: [Character]) -> Bool { 24 var i = 0, j = 0 25 while i<chars1.count && j<chars2.count { 26 if chars1[i] == chars2[j] { 27 j += 1 28 } 29 i += 1 30 } 31 return j == chars2.count 32 } 33 34 func getDuplicates(_ strs: [String]) -> Set<String> { 35 var set = Set<String>() 36 var duplicates = Set<String>() 37 for s in strs { 38 if set.contains(s) { 39 duplicates.insert(s) 40 }else { 41 set.insert(s) 42 } 43 } 44 return duplicates 45 } 46 }