Skip to content

Commit

Permalink
Refactored solutions 259 - 260
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 31, 2021
1 parent 9c355b0 commit ca8b139
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
43 changes: 30 additions & 13 deletions Solutions/259.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
"""
Problem:
Ghost is a two-person word game where players alternate appending letters to a word. The first person who spells out a word, or creates a prefix for which there is no possible continuation, loses. Here is a sample game:
Ghost is a two-person word game where players alternate appending letters to a word.
The first person who spells out a word, or creates a prefix for which there is no
possible continuation, loses. Here is a sample game:
Player 1: g
Player 2: h
Player 1: o
Player 2: s
Player 1: t [loses]
Given a dictionary of words, determine the letters the first player should start with, such that with optimal play they cannot lose.
Given a dictionary of words, determine the letters the first player should start with,
such that with optimal play they cannot lose.
For example, if the dictionary is ["cat", "calf", "dog", "bear"], the only winning start letter would be b.
For example, if the dictionary is ["cat", "calf", "dog", "bear"], the only winning
start letter would be b.
"""


def get_winning_letters(words):
# creating a map of starting characters to the words
from typing import List, Set


def get_winning_letters(words: List[str]) -> Set[str]:
# requirements for winning start letter:
# - the length is even for all words starting with the character
starting_char_freq = {}
for word in words:
if word[0] not in starting_char_freq:
starting_char_freq[word[0]] = []
starting_char_freq[word[0]].append(word)
# getting the winning start characters
# requirements: only 1 word starts with the character & its length is even

winning_start_letters = set()
for starting_char in starting_char_freq:
if len(starting_char_freq[starting_char]) == 1:
if len(starting_char_freq[starting_char][0]) % 2 == 0:
winning_start_letters.add(starting_char)
for word in starting_char_freq[starting_char]:
if len(word) % 2 != 0:
break
else:
winning_start_letters.add(starting_char)
return winning_start_letters


# DRIVER CODE
print(get_winning_letters(["cat", "calf", "dog", "bear"]))
print(get_winning_letters(["cat", "something", "hi", "calf", "dog", "bear"]))
if __name__ == "__main__":
print(get_winning_letters(["cat", "calf", "dog", "bear"]))
print(get_winning_letters(["something", "hi", "cat", "dog", "bear", "hola"]))


"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""
24 changes: 18 additions & 6 deletions Solutions/260.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"""
Problem:
The sequence [0, 1, ..., N] has been jumbled, and the only clue you have for its order is an array representing whether each number is larger or smaller than the last. Given this information, reconstruct an array that is consistent with it. For example, given [None, +, +, -, +], you could return [1, 2, 3, 0, 4].
The sequence [0, 1, ..., N] has been jumbled, and the only clue you have for its order
is an array representing whether each number is larger or smaller than the last. Given
this information, reconstruct an array that is consistent with it. For example, given
[None, +, +, -, +], you could return [1, 2, 3, 0, 4].
"""


def get_sequence(relative_arr):
# getting the number of '+' and generating the first number
from typing import List, Optional


def get_sequence(relative_arr: List[Optional[str]]) -> List[int]:
length = len(relative_arr)
larger_count = relative_arr.count("+")
first_num = length - 1 - larger_count
larger_num, smaller_num = first_num + 1, first_num - 1

# generating the result array
result = [first_num]
for elem in relative_arr[1:]:
if elem == "+":
Expand All @@ -24,5 +28,13 @@ def get_sequence(relative_arr):
return result


# DRIVER CODE
print(get_sequence([None, "+", "+", "-", "+"]))
if __name__ == "__main__":
print(get_sequence([None, "+", "+", "-", "+"]))


"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""

0 comments on commit ca8b139

Please sign in to comment.