[Swift]LeetCode383. 赎金信 | Ransom Note
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9775114.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。
(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)
注意:
你可以假设两个字符串均只含有小写字母。
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true
1 class Solution { 2 func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { 3 //把英文字符都存入数组中,最后判断数组的每一个位置是否存在<0即可 4 var arr = Array(repeating: 0,count:26) 5 for index in magazine.indices 6 { 7 var char:Character = magazine[index] 8 for asc in char.unicodeScalars 9 { 10 var num:Int = Int(asc.value - 97) 11 arr[num] += 1 12 } 13 } 14 for index in ransomNote.indices 15 { 16 var char:Character = ransomNote[index] 17 for asc in char.unicodeScalars 18 { 19 var num:Int = Int(asc.value - 97) 20 arr[num] -= 1 21 if arr[num] < 0 22 { 23 return false 24 } 25 } 26 } 27 return true 28 29 } 30 }
28ms
1 class Solution { 2 func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { 3 guard ransomNote.count <= magazine.count else { return false } 4 5 var alphabetArray = [Int].init(repeating: 0, count: 26) 6 var count = 0 7 8 // Add letters from ransomNote to alphabetArray, incrementing count for each letter 9 for r in ransomNote.unicodeScalars { 10 alphabetArray[Int(r.value) - 97] += 1 11 count += 1 12 } 13 14 // Remove letters from magazine already in alphabetArray, decrementing count for each letter found 15 for m in magazine.unicodeScalars { 16 if alphabetArray[Int(m.value) - 97] > 0 { 17 // valid existing letter from ransomNote 18 alphabetArray[Int(m.value) - 97] -= 1 19 count -= 1 20 } 21 22 // If count == 0, we have a match 23 if count == 0 { break } 24 } 25 26 return count == 0 27 } 28 }
52ms
1 class Solution { 2 func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { 3 var words = [Int](repeating: 0, count: 26) 4 let indexChar = "a" 5 let indexComp = Int(indexChar.unicodeScalars.first!.value) 6 for char in magazine.unicodeScalars { 7 let index = Int(char.value) - indexComp 8 // var count = words[index] ?? 0 9 words[index] += 1 10 // words[index] = count 11 } 12 13 for char in ransomNote.unicodeScalars { 14 let index = Int(char.value) - indexComp 15 if words[index] <= 0 {return false} 16 words[index] -= 1 17 } 18 return true 19 } 20 }
64ms
1 class Solution { 2 func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { 3 4 var magCounts = Array(repeating: 0, count: 256) 5 for each in magazine.utf8{ 6 magCounts[Int(each)] += 1 7 } 8 9 for each in ransomNote.utf8{ 10 if magCounts[Int(each)] == 0 { 11 return false 12 }else{ 13 magCounts[Int(each)] -= 1 14 } 15 } 16 17 return true 18 19 } 20 }
76ms
1 class Solution { 2 func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { 3 guard magazine.count >= ransomNote.count else { 4 return false 5 } 6 var magazine = magazine 7 8 for char in ransomNote { 9 if let index = magazine.index(of: char) { 10 magazine.remove(at: index) 11 } else { 12 return false 13 } 14 } 15 16 return true 17 } 18 }
84ms
1 class Solution { 2 func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { 3 guard ransomNote.count <= magazine.count else { 4 return false 5 } 6 var magazine = magazine 7 for c in ransomNote { 8 if let index = magazine.index(of: c) { 9 magazine.remove(at: index) 10 } else { 11 return false 12 } 13 } 14 15 return true 16 } 17 }