Thursday, December 22, 2022
Ransom note
A function to determine if ransomNote
can be constructed by using the letters
from magazine
, each of them is used only once.
Go
- Time complexity: - n is a length of the first string, k is a length of the second string (if it's larger than n)
- Auxiliary space: - constant amount of space
import "strings"
func canConstruct(ransomNote string, magazine string) bool {
for _, rune := range ransomNote {
frequencyInRansomNote := strings.Count(ransomNote, string(rune))
frequencyInMagazine := strings.Count(magazine, string(rune))
if (frequencyInMagazine < frequencyInRansomNote) {
return false
}
}
return true
}