""" 0001.1 - Solution 1 - Brute Force Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0001.1 - Solution 1 - Using Enumerate """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for key1, num1 in enumerate(nums):
for key2, num2 in enumerate(nums[key1 + 1 :], key1 + 1):
# enumerate(iterable, start=1) -> start value determines starting index
# Printing value and key will output 1 i , 2 j, 3 k, 4 l
if num1 + num2 == target:
return [key1, key2]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1684.1 - Count the Number of Consistent Strings - Solution 1 - Brute Force using Set """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
"""Count Consistent Strings Function"""
allowed_set = set(allowed)
count = 0
for word in words:
is_consistent = True
for char in word:
if char not in allowed_set:
is_consistent = False
break
if is_consistent:
count += 1
return count
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().countConsistentStrings("ab", ["ad", "bd", "aaab", "baa", "badab"]))
# Expected: 2
print(Solution().countConsistentStrings("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]))
# Expected: 7
print(Solution().countConsistentStrings("cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"]))
# Expected: 4
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1684.2 - Count the Number of Consistent Strings - Solution 2 - Pythonic using all() """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
"""Count Consistent Strings Function"""
allowed_set = set(allowed)
return sum(all(c in allowed_set for c in word) for word in words)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().countConsistentStrings("ab", ["ad", "bd", "aaab", "baa", "badab"]))
# Expected: 2
print(Solution().countConsistentStrings("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]))
# Expected: 7
print(Solution().countConsistentStrings("cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"]))
# Expected: 4
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1684.3 - Count the Number of Consistent Strings - Solution 3 - Set Subset Check """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
"""Count Consistent Strings Function"""
allowed_set = set(allowed)
return sum(set(word) <= allowed_set for word in words)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().countConsistentStrings("ab", ["ad", "bd", "aaab", "baa", "badab"]))
# Expected: 2
print(Solution().countConsistentStrings("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]))
# Expected: 7
print(Solution().countConsistentStrings("cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"]))
# Expected: 4
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1684.4 - Count the Number of Consistent Strings - Solution 4 - Bitmask Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
"""Count Consistent Strings Function"""
allowed_mask = 0
for c in allowed:
allowed_mask |= 1 << (ord(c) - ord('a'))
count = 0
for word in words:
word_mask = 0
for c in word:
word_mask |= 1 << (ord(c) - ord('a'))
if word_mask & allowed_mask == word_mask:
count += 1
return count
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().countConsistentStrings("ab", ["ad", "bd", "aaab", "baa", "badab"]))
# Expected: 2
print(Solution().countConsistentStrings("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]))
# Expected: 7
print(Solution().countConsistentStrings("cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"]))
# Expected: 4
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()