Given a ransom channel (ransom) and a string magazine (magazine) string, the first string is determined ransom can consists of a second character string inside the magazines. If you can constitute, return true; otherwise false.
(Title Description: In order not to expose ransom letter writing, from the search needs of each magazine letters to form words to express the meaning.)
note:
You can assume two strings contain only lowercase letters.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
solution:
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
if ransomNote.count > 0 && magazine.count > 0 {
}else if ransomNote.count == 0 && magazine.count == 0 {
return true
}else if ransomNote.count == 0 && magazine.count > 0{
return true
}else{
return false
}
var magazine = magazine
for item in ransomNote {
if let index = magazine.firstIndex(of: item) {
magazine.remove(at: index)
}else{
return false
}
}
return true
}