-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateDocument.py
More file actions
24 lines (19 loc) · 702 Bytes
/
GenerateDocument.py
File metadata and controls
24 lines (19 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'''
Generate Document:
Given an array of characters and a target document, determine if the
character array has the necessary characters (including count), to
create the document
Time: O(C + D) -> O(N), where C=characters and D=document
Space: O(C) -> O(N), where N is the size of the character dictionary
'''
def generateDocument(characters, document):
chars = {}
for character in characters:
if character not in chars:
chars[character] = 0
chars[character] += 1
for character in document:
if character not in chars or chars[character] == 0:
return False
chars[character] -= 1
return True