[Swift]LeetCode1002. 查找常用字符 | Find Common Characters
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10464627.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given an array A
of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.
You may return the answer in any order.
Example 1:
Input: ["bella","label","roller"]
Output: ["e","l","l"]
Example 2:
Input: ["cool","lock","cook"]
Output: ["c","o"]
Note:
1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j]
is a lowercase letter
给定仅有小写字母组成的字符串数组 A
,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。
你可以按任意顺序返回答案。
示例 1:
输入:["bella","label","roller"] 输出:["e","l","l"]
示例 2:
输入:["cool","lock","cook"] 输出:["c","o"]
提示:
1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j]
是小写字母
1 class Solution { 2 func commonChars(_ A: [String]) -> [String] { 3 if A.count < 1 { 4 return [] 5 } 6 7 var commonCount: [Int] = Array.init(repeating: 0, count: 26) 8 var ans:[String] = [] 9 for string in A { 10 var thisFrequency:[Int] = Array.init(repeating: 0, count: 26) 11 var leftIndex = string.startIndex 12 13 while leftIndex < string.endIndex { 14 let leftChar = string[leftIndex] 15 if let value = leftChar.unicodeScalars.first?.value { 16 let index = Int(value - 97) 17 thisFrequency[index] += 1 18 } 19 // move towards middle 20 leftIndex = string.index(after: leftIndex) 21 } 22 if string == A.first { 23 for index in 0...25 { 24 commonCount[index] = thisFrequency[index] 25 } 26 } else { 27 for index in 0...25 { 28 commonCount[index] = min(commonCount[index], thisFrequency[index]) 29 } 30 } 31 } 32 33 for index in 0...25 { 34 while commonCount[index] > 0 { 35 let value = index + 97 36 if let scalar = UnicodeScalar(value) { 37 let character = Character(scalar) 38 ans.append("\(character)") 39 } 40 commonCount[index] -= 1 41 } 42 } 43 44 return ans 45 } 46 }
68ms
1 class Solution { 2 func commonChars(_ words: [String]) -> [String] { 3 var prevDict = [String:Int]() 4 var currDict = [String:Int]() 5 6 // Get the first word, otherwise return 7 guard let firstWord = words.first else { 8 return [] 9 } 10 11 // Add chars of first word to result 12 for char in firstWord { 13 let char = String(char) 14 if let prevFreq = prevDict[char] { 15 prevDict[char] = prevFreq + 1 16 } 17 else { 18 prevDict[char] = 1 19 } 20 } 21 22 for word in words[1...] { 23 for char in word { 24 //char is in previous dictionary 25 //Add to current 26 let char = String(char) 27 if let prevFreq = prevDict[char] { 28 if let currentFreq = currDict[char] { 29 currDict[char] = currentFreq < prevFreq ? currentFreq + 1 : prevFreq 30 } 31 else { 32 currDict[char] = 1 33 } 34 } 35 } 36 37 //Setup for next iteration 38 prevDict = currDict 39 currDict.removeAll() 40 } 41 42 var result = [String]() 43 for (char, freq) in prevDict { 44 for _ in 0..<freq { 45 result.append(char) 46 } 47 } 48 49 return result 50 } 51 }
72ms
1 class Solution { 2 func commonChars(_ A: [String]) -> [String] { 3 var repeatCount: [Int] = Array.init(repeating: 0, count: 26) 4 var outPutString = [String]() 5 let aChar: String = "a" 6 7 var count: Int = 0 8 for eachString in A { 9 var onceCount: [Int] = Array.init(repeating: 0, count: 26) 10 for char in eachString { 11 let acharNUm: Int = aChar.transInToAcsIIInt() 12 let charNUm: Int = String(char).transInToAcsIIInt() 13 let counter: Int = charNUm - acharNUm 14 if count == 0 { 15 repeatCount[counter] += 1 16 } else { 17 onceCount[counter] += 1 18 } 19 } 20 for index in 0..<26 { 21 if count != 0 { 22 repeatCount[index] = min(repeatCount[index], onceCount[index]) 23 } 24 } 25 count += 1 26 } 27 28 count = 0 29 for eachNum in repeatCount { 30 for _ in 0..<eachNum { 31 outPutString.append(String(Character(UnicodeScalar(aChar.transInToAcsIIInt() + count)!))) 32 } 33 count += 1 34 } 35 36 return outPutString; 37 } 38 } 39 40 41 42 extension String { 43 func transInToAcsIIInt() -> Int { 44 var acharNUm: Int = 0 45 for num in self.unicodeScalars { 46 acharNUm = Int(num.value) 47 } 48 49 return acharNUm 50 } 51 }
76ms
1 class Solution { 2 func commonChars(_ A: [String]) -> [String] { 3 let ref = A[0] 4 var refDict = [Character: Int]() 5 for char in ref { 6 refDict[char, default: 0] += 1 7 } 8 9 for i in 1..<A.count { 10 let str = A[i] 11 var dict = [Character: Int]() 12 for char in str { 13 dict[char, default: 0] += 1 14 } 15 16 for char in refDict.keys { 17 if let count = dict[char] { 18 refDict[char] = min(refDict[char]!, count) 19 } else { 20 refDict[char] = nil 21 } 22 } 23 } 24 25 var result = [String]() 26 for (char, count) in refDict { 27 for _ in 0..<count { 28 result.append(String(char)) 29 } 30 } 31 return result 32 } 33 }
80ms
1 class Solution { 2 func commonChars(_ A: [String]) -> [String] { 3 func f(A: [[Character]], result: [String]) -> [String] { 4 if (A.count == 0) { return result } 5 var newA = A 6 var currentWord = newA.removeFirst().map{ String($0) } 7 var i1 = 0 8 var res = [String]() 9 while i1 < result.count { 10 let f_char = result[i1] 11 if let founded_char_index = currentWord.firstIndex(of: f_char) { 12 res.append(String(f_char)) 13 currentWord.remove(at: founded_char_index) 14 } 15 i1 += 1 16 } 17 return f(A: newA, result: res) 18 } 19 var strings = A.map{Array(String($0))} 20 let result = strings.removeFirst().map{ String($0) } 21 return f(A: strings, result: result) 22 } 23 }
Runtime: 88 ms
1 class Solution { 2 func commonChars(_ A: [String]) -> [String] { 3 var n:Int = A.count 4 var h:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:26),count:n) 5 for i in 0..<n 6 { 7 for j in 0..<A[i].count 8 { 9 h[i][A[i][j].ascii - 97] += 1 10 } 11 } 12 var ans:[String] = [String]() 13 for j in 0..<26 14 { 15 var num:Int = 10000 16 for i in 0..<n 17 { 18 num = min(h[i][j], num) 19 } 20 var cs:String = String((j + 97).ASCII) 21 for i in 0..<num 22 { 23 ans.append(cs) 24 } 25 } 26 return ans 27 } 28 } 29 30 //String扩展 31 extension String { 32 //subscript函数可以检索数组中的值 33 //直接按照索引方式截取指定索引的字符 34 subscript (_ i: Int) -> Character { 35 //读取字符 36 get {return self[index(startIndex, offsetBy: i)]} 37 } 38 } 39 40 //Character扩展 41 extension Character 42 { 43 //转ASCII整数值(定义小写为整数值) 44 var ascii: Int { 45 get { 46 return Int(self.unicodeScalars.first!.value) 47 } 48 } 49 } 50 51 //Int扩展 52 extension Int 53 { 54 //属性:ASCII值(定义大写为字符值) 55 var ASCII:Character 56 { 57 get {return Character(UnicodeScalar(self)!)} 58 } 59 }
92ms
1 class Solution { 2 func commonChars(_ A: [String]) -> [String] { 3 var results = [String]() 4 guard A.count > 0 else { return results } 5 var masterMap = [String : Int]() 6 for char in A[0] { 7 let s = String(char) 8 guard masterMap[s] != nil else { 9 masterMap[s] = 1 10 continue 11 } 12 masterMap[s]! += 1 13 } 14 for i in 1..<A.count { 15 guard masterMap.count > 0 else { return results } 16 var tempMap = [String : Int]() 17 for char in A[i] { 18 let s = String(char) 19 guard tempMap[s] != nil else { 20 tempMap[s] = 1 21 continue 22 } 23 tempMap[s]! += 1 24 } 25 for key in masterMap.keys { 26 guard let temp = tempMap[key], let master = masterMap[key] else { 27 masterMap.removeValue(forKey: key) 28 continue 29 } 30 masterMap[key] = min(master, temp) 31 } 32 } 33 for (key, value) in masterMap { 34 for _ in 0..<value { 35 results.append(key) 36 } 37 } 38 return results 39 } 40 }
100ms
1 struct CharCounter 2 { 3 var countOf: [ Character : Int ] 4 init() 5 { 6 self.countOf = [ : ] 7 } 8 9 mutating func insert( _ char: Character ) 10 { 11 if let count: Int = countOf[ char ] 12 { 13 countOf[ char ] = ( count + 1 ) 14 } 15 else 16 { 17 countOf[ char ] = 1 18 } 19 } 20 21 func countOf( _ char: Character ) -> Int 22 { 23 if let count: Int = countOf[ char ] 24 { 25 return count 26 } 27 else 28 { 29 return 0 30 } 31 } 32 } 33 34 class Solution 35 { 36 func commonChars( _ strings: [ String ] ) -> [ String ] 37 { 38 var charSets: [ CharCounter ] = [ CharCounter ].init( 39 repeating: CharCounter(), 40 count: strings.count 41 ) 42 var index: Int = 0 43 while index < strings.count 44 { 45 let string = strings[ index ] 46 47 for char in string 48 { 49 charSets[ index ].insert( char ) 50 } 51 index += 1 52 } 53 var result: [ String ] = [] 54 55 for char in charSets[ 0 ].countOf.keys 56 { 57 58 var minCount: Int = charSets[ 0 ].countOf( char ) 59 60 var index: Int = 1 61 while index < charSets.count 62 { 63 minCount = min( minCount, charSets[ index ].countOf( char ) ) 64 index += 1 65 } 66 67 if minCount > 0 68 { 69 for _ in ( 1 ... minCount ) 70 { 71 result.append( String( char ) ) 72 } 73 } 74 } 75 return result 76 } 77 }