Golang solution with array


  • 0
    R
    func canConstruct(ransomNote string, magazine string) bool {
        dickey := make([]int, 26, 26)
        
        for i:=0; i < len(magazine); i++ {
            dickey[int(magazine[i]) - int('a')] += 1  
        }
        
        for j:=0; j < len(ransomNote); j++ {
            dickey[int(ransomNote[j]) - int('a')] -= 1  
            if dickey[int(ransomNote[j]) - int('a')] < 0 {
                    return false
            }
        }
        
        return true
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.