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,22 @@
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
@@ -0,0 +1,32 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
from collections import deque, defaultdict

class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
result = []

def backtrack(start: int, current: List[int], remaining: int):
# Base case: found a valid combination
if remaining == 0:
result.append(current[:])
return

# Base case: exceeded target
if remaining < 0:
return

# Explore all candidates starting from 'start' index
for i in range(start, len(candidates)):
# Include candidates[i] in the current combination
current.append(candidates[i])

# Recurse with the same start index (we can reuse the same number)
backtrack(i, current, remaining - candidates[i])

# Backtrack: remove the last added element
current.pop()

backtrack(0, [], target)
return result

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = [];

function backtrack(start: number, current: number[], remaining: number): void {
// Base case: found a valid combination
if (remaining === 0) {
result.push([...current]);
return;
}

// Base case: exceeded target
if (remaining < 0) {
return;
}

// Explore all candidates starting from 'start' index
for (let i = start; i < candidates.length; i++) {
// Include candidates[i] in the current combination
current.push(candidates[i]);

// Recurse with the same start index (we can reuse the same number)
backtrack(i, current, remaining - candidates[i]);

// Backtrack: remove the last added element
current.pop();
}
}

backtrack(0, [], target);
return result;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import unittest
from typing import Optional, List
from src.my_project.interviews.top_150_questions_round_22\
.ex_99_combination_sum import Solution


class CombinationSumTestCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_example1(self):
"""Test case: candidates = [2,3,6,7], target = 7"""
candidates = [2, 3, 6, 7]
target = 7
result = self.solution.combinationSum(candidates, target)
expected = [[2, 2, 3], [7]]
self.assertEqual(sorted([sorted(x) for x in result]),
sorted([sorted(x) for x in expected]))

def test_example2(self):
"""Test case: candidates = [2,3,5], target = 8"""
candidates = [2, 3, 5]
target = 8
result = self.solution.combinationSum(candidates, target)
expected = [[2, 2, 2, 2], [2, 3, 3], [3, 5]]
self.assertEqual(sorted([sorted(x) for x in result]),
sorted([sorted(x) for x in expected]))

def test_example3(self):
"""Test case: candidates = [2], target = 1"""
candidates = [2]
target = 1
result = self.solution.combinationSum(candidates, target)
expected = []
self.assertEqual(result, expected)

def test_single_element_exact(self):
"""Test case: single element that equals target"""
candidates = [5]
target = 5
result = self.solution.combinationSum(candidates, target)
expected = [[5]]
self.assertEqual(result, expected)

def test_multiple_use_same_number(self):
"""Test case: using same number multiple times"""
candidates = [3]
target = 9
result = self.solution.combinationSum(candidates, target)
expected = [[3, 3, 3]]
self.assertEqual(result, expected)
Loading