-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_ransom_note.py
44 lines (33 loc) · 1.87 KB
/
3_ransom_note.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
##########################################################
# Author: Raghav Sikaria
# LinkedIn: https://www.linkedin.com/in/raghavsikaria/
# Github: https://github.com/raghavsikaria
# Last Update: 3-5-2020
# Project: LeetCode May 31 Day 2020 Challenge - Day 3
##########################################################
# QUESTION
# This question is from the abovementioned challenge and can be found here on LeetCode: https://leetcode.com/explore/featured/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3318/
############################################################################
# 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
############################################################################
# ACCEPTED SOLUTION
# This solution ensures O(n) Time and O(1) Space Complexity for Large strings
class Solution:
def populate_frequency_array(self,given_string,lcase_array):
for character in given_string:
lcase_array[ord(character) - 97] += 1
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
answer = True
r_lcase_freq_array = [0]*26
m_lcase_freq_array = [0]*26
self.populate_frequency_array(ransomNote,r_lcase_freq_array)
self.populate_frequency_array(magazine,m_lcase_freq_array)
for alphabet in range(0,26):
if r_lcase_freq_array[alphabet] > m_lcase_freq_array[alphabet]: answer = False
return answer