Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 []

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
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
Original file line number Diff line number Diff line change
@@ -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
Loading