From ac89463e6a78edd3c60c3328ed6881d871eef529 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 21 Jun 2026 05:27:51 -0600 Subject: [PATCH] adding updates --- .../common_algos/two_sum_round_7.py | 17 +++++++ .../common_algos/valid_palindrome_round_7.py | 22 ++++++++ .../number_of_ways_to_select_building.py | 50 ++++++++----------- .../number_of_ways_to_select_building.py | 32 ++++++++++++ 4 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_6/number_of_ways_to_select_building.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py new file mode 100644 index 00000000..5311024e --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py @@ -0,0 +1,17 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + answer = dict() + + for k, v in enumerate(nums): + + if v in answer: + return [answer[v], k] + else: + answer[target - v] = k + + return [] + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py new file mode 100644 index 00000000..dff42da9 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # To lowercase + s = s.lower() + + # Remove non-alphanumeric characters + s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) + + # Determine if s is palindrome or not + len_s = len(s) + + for i in range(len_s//2): + + if s[i] != s[len_s - 1- i]: + return False + + return True diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/number_of_ways_to_select_building.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/number_of_ways_to_select_building.py index ce57420f..693f7f60 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/number_of_ways_to_select_building.py +++ b/src/my_project/interviews/amazon_high_frequency_23/round_2/number_of_ways_to_select_building.py @@ -10,33 +10,23 @@ def numberOfWays(self, s: str) -> int: then extend them with the appropriate third character. Time: O(n), Space: O(1) - """ - count_0 = 0 # Count of '0' seen so far - count_1 = 0 # Count of '1' seen so far - count_01 = 0 # Count of "01" patterns (0 followed by 1) - count_10 = 0 # Count of "10" patterns (1 followed by 0) - - result = 0 - - for char in s: - if char == '0': - # Current '0' can complete "010": we need "01" before this '0' - result += count_01 - - # Current '0' can start new "01" patterns: pair with each previous '0' - # Wait no, "01" means 0 then 1, so we can't create "01" with another 0 - - # Current '0' extends all previous '1' to form "10" pattern - count_10 += count_1 - - count_0 += 1 - else: # char == '1' - # Current '1' can complete "101": we need "10" before this '1' - result += count_10 - - # Current '1' extends all previous '0' to form "01" pattern - count_01 += count_0 - - count_1 += 1 - - return result \ No newline at end of file + """ + total_zeros = s.count('0') + total_ones = len(s) - total_zeros + + left_zeros = left_ones = 0 + ways = 0 + + for ch in s: + if ch == '1': + # middle is '1' -> need '0' on both sides -> "010" + right_zeros = total_zeros - left_zeros + ways += left_zeros * right_zeros + left_ones += 1 + else: # ch == '0' + # middle is '0' -> need '1' on both sides -> "101" + right_ones = total_ones - left_ones + ways += left_ones * right_ones + left_zeros += 1 + + return ways \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_6/number_of_ways_to_select_building.py b/src/my_project/interviews/amazon_high_frequency_23/round_6/number_of_ways_to_select_building.py new file mode 100644 index 00000000..693f7f60 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_6/number_of_ways_to_select_building.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def numberOfWays(self, s: str) -> int: + """ + Count valid 3-building selections forming "010" or "101" patterns. + + Key insight: Track how many 2-character patterns we can form, + then extend them with the appropriate third character. + + Time: O(n), Space: O(1) + """ + total_zeros = s.count('0') + total_ones = len(s) - total_zeros + + left_zeros = left_ones = 0 + ways = 0 + + for ch in s: + if ch == '1': + # middle is '1' -> need '0' on both sides -> "010" + right_zeros = total_zeros - left_zeros + ways += left_zeros * right_zeros + left_ones += 1 + else: # ch == '0' + # middle is '0' -> need '1' on both sides -> "101" + right_ones = total_ones - left_ones + ways += left_ones * right_ones + left_zeros += 1 + + return ways \ No newline at end of file