Skip to content

Commit

Permalink
Refactored solutions 231 - 232
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 29, 2021
1 parent fbcc592 commit 1dccdcc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 59 deletions.
97 changes: 49 additions & 48 deletions Solutions/231.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,80 @@
"""
Problem:
Given a string with repeated characters, rearrange the string so that no two adjacent characters are the same.
If this is not possible, return None.
Given a string with repeated characters, rearrange the string so that no two adjacent
characters are the same. If this is not possible, return None.
Example:
For example, given "aaabbc", you could return "ababac". Given "aaab", return None.
"""

Input = "aaabbc"
Output = "ababac"
from typing import Optional

Input = "aaab"
Output = None
"""
from DataStructures.Queue import Queue


def get_unique_adjacent(string):
def get_unique_adjacent(string: str) -> Optional[str]:
length = len(string)
freq = {}

if length == 0:
return string

# getting the character frequency
for i in range(length):
if string[i] in freq:
freq[string[i]] += 1
else:
freq[string[i]] = 1
if string[i] not in freq:
freq[string[i]] = 0
freq[string[i]] += 1

sorted_freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
queue = list(sorted_freq)

# checking if a desired string can be formed
if length % 2 == 0:
if sorted_freq[0][1] > length // 2:
return None
else:
if sorted_freq[0][1] > (length // 2) + 1:
return None
queue = Queue()
[queue.enqueue(item) for item in sorted_freq]
result = ""

res = ""
if length % 2 == 0 and sorted_freq[0][1] > length // 2:
return None
elif length % 2 == 1 and sorted_freq[0][1] > (length // 2) + 1:
return None

# creating the required string
while queue:
while not queue.is_empty():
if len(queue) == 1:
if queue[0][1] == 2:
res = queue[0][0] + res + queue[0][0]
elem, freq = queue.peek()
if freq == 2:
result = elem + result + elem
break
elif queue[0][1] == 1:
if res[-1] != queue[0][0]:
res += queue[0][0]
elif freq == 1:
if result[-1] != elem:
result += elem
else:
res = queue[0][0] + res
result = elem + result
break
else:
return None
return None

res += queue[0][0] + queue[1][0]
elem1, freq1 = queue.peek()
elem2, freq2 = None, None
if len(queue) > 1:
elem2, freq2 = queue[1]

queue[0] = queue[0][0], queue[0][1] - 1
queue[1] = queue[1][0], queue[1][1] - 1
result += elem1 + elem2

queue[0] = elem1, freq1 - 1
if len(queue) > 1:
queue[1] = elem2, freq2 - 1
if len(queue) > 1 and queue[1][1] == 0:
queue.pop(1)
queue.dequeue()
if len(queue) > 0 and queue[0][1] == 0:
queue.pop(0)
queue.dequeue()
return result

return res

if __name__ == "__main__":
print(get_unique_adjacent("aaabbc"))
print(get_unique_adjacent("aaabbcc"))
print(get_unique_adjacent("aaabbac"))
print(get_unique_adjacent("aaab"))
print(get_unique_adjacent("aaabbaa"))

# DRIVER CODE
print(get_unique_adjacent("aaabbc"))
print(get_unique_adjacent("aaabbcc"))
print(get_unique_adjacent("aaabbac"))

# cannot form a word of the desired form
print(get_unique_adjacent("aaab"))
print(get_unique_adjacent("aaabbaa"))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""
25 changes: 14 additions & 11 deletions Solutions/232.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
Problem:
Implement a PrefixMapSum class with the following methods:
* insert(key: str, value: int): Set a given key's value in the map. If the key already exists, overwrite the value.
* sum(prefix: str): Return the sum of all values of keys that begin with a given prefix.
insert(key: str, value: int): Set a given key's value in the map. If the key already
exists, overwrite the value.
sum(prefix: str): Return the sum of all values of keys that begin with a given prefix.
For example, you should be able to run the following code:
>>> mapsum.insert("columnar", 3)
>>> assert mapsum.sum("col") == 3
>>> mapsum.insert("column", 2)
Expand All @@ -16,28 +19,28 @@


class PrefixMapSum:
def __init__(self):
def __init__(self) -> None:
self.trie = Trie()
self.hash_map = {}

def insert(self, key, value):
def insert(self, key: str, value: int) -> None:
if key not in self.hash_map:
self.trie.add(key)
self.hash_map[key] = value

def sum(self, prefix):
def sum(self, prefix: str) -> int:
words = self.trie.get_suggestions(prefix)
result = 0
for word in words:
result += self.hash_map[word]
return result


# DRIVER CODE
mapsum = PrefixMapSum()
if __name__ == "__main__":
mapsum = PrefixMapSum()

mapsum.insert("columnar", 3)
print(mapsum.sum("col"))
mapsum.insert("columnar", 3)
assert mapsum.sum("col") == 3

mapsum.insert("column", 2)
print(mapsum.sum("col"))
mapsum.insert("column", 2)
assert mapsum.sum("col") == 5

0 comments on commit 1dccdcc

Please sign in to comment.