diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrom_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrom_round_3.py new file mode 100644 index 00000000..0f3740de --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrom_round_3.py @@ -0,0 +1,21 @@ +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='[^a-zA-Z0-9]', repl='', string=s) + + # Check if it 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/top_150_questions_round_21/longest_common_prefix.py b/src/my_project/interviews/top_150_questions_round_21/longest_common_prefix.py new file mode 100644 index 00000000..509c29d1 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/longest_common_prefix.py @@ -0,0 +1,20 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + + if not strs: + return '' + + min_strs, max_strs = min(strs), max(strs) + count = 0 + + for i in range(len(min_strs)): + + if min_strs[i] == max_strs[i]: + count +=1 + else: + break + + return min_strs[:count] diff --git a/tests/test_150_questions_round_21/test_longest_common_prefix_round_21.py b/tests/test_150_questions_round_21/test_longest_common_prefix_round_21.py new file mode 100644 index 00000000..2e7bffb9 --- /dev/null +++ b/tests/test_150_questions_round_21/test_longest_common_prefix_round_21.py @@ -0,0 +1,23 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.longest_common_prefix import Solution + +class LongestCommonPrefixTestCase(unittest.TestCase): + + def test_longest_common_prefix(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=["flower","flow","flight"]) + target = 'fl' + self.assertEqual(target, output) + + def test_longest_no_common_prefix(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=["dog","racecar","car"]) + target = '' + self.assertEqual(target, output) + + def test_longest_common_prefix_null_list(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=[]) + target = '' + self.assertEqual(target, output)