diff --git a/easy/distribute-candies.py b/easy/distribute-candies.py new file mode 100644 index 0000000..7a96191 --- /dev/null +++ b/easy/distribute-candies.py @@ -0,0 +1,15 @@ +#!/usr/bin/python +# yigid balaban +# neetcode 2024 +# easy / distribute candies + +def solution(candyType: list[int]) -> int: + howManyTypes = len(set(candyType)) + permitAmount = len(candyType) // 2 + return permitAmount if howManyTypes >= permitAmount else howManyTypes + + +print(solution([1,1,2,2,3,3]) == 3) +print(solution([1,1,2,3]) == 2) +print(solution([6,6,6,6]) == 1) + diff --git a/easy/find-the-index-of-the-first-occurence-in-string.py b/easy/find-the-index-of-the-first-occurence-in-string.py new file mode 100644 index 0000000..9dc6988 --- /dev/null +++ b/easy/find-the-index-of-the-first-occurence-in-string.py @@ -0,0 +1,35 @@ +#!/usr/bin/python +# yigid balaban +# neetcode 2024 +# easy / find the first occurence in a string + +# TC: O(n), MC: O(1) +def solution(haystack: str, needle: str) -> int: + ph, pn = 0, 0 + position = -1 + while ph < len(haystack): + print(ph, haystack[ph], pn, needle[pn]) + if haystack[ph] == needle[pn]: + if position == -1: + position = ph + pn += 1 + if pn == len(needle): + break + else: + if pn != 0: + ph = position + position, pn = -1, 0 + ph += 1 + return position if pn == len(needle) else -1 + + +print(solution('sadbutsad', 'sad') == 0) +print(solution('leetcode', 'lecola') == -1) +print(solution('leetcode', 'etc') == 2) +print(solution('a', 'a') == 0) +print(solution('bbbab', 'ab') == 3) +print(solution('bbbaaaab', 'ab') == 6) +print(solution('aa', 'aaa') == -1) +print(solution('a', 'aaa') == -1) +print(solution('mississippi', 'issip') == 4) + diff --git a/easy/palindrome-number.py b/easy/palindrome-number.py new file mode 100644 index 0000000..8d94cb6 --- /dev/null +++ b/easy/palindrome-number.py @@ -0,0 +1,13 @@ +#!/usr/bin/python +# yigid balaban +# neetcode 2024 +# easy / palindrome number + +def solution(x: int) -> bool: + return str(x) == str(x)[::-1] + + +print(solution(121) == True) +print(solution(0) == True) +print(solution(-121) == False) +print(solution(10) == False)