diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_38_ransom_note.py b/src/my_project/interviews/top_150_questions_round_22/ex_38_ransom_note.py new file mode 100644 index 00000000..adb3bb13 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_38_ransom_note.py @@ -0,0 +1,11 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def canConstruct(self,ransomNote: str, magazine: str) -> bool: + + for c in set(ransomNote): + if ransomNote.count(c) > magazine.count(c): + return False + + return True \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_39_isomorphic_strings.py b/src/my_project/interviews/top_150_questions_round_22/ex_39_isomorphic_strings.py new file mode 100644 index 00000000..2e2a555f --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_39_isomorphic_strings.py @@ -0,0 +1,7 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def isIsomorphic(self, s: str, t: str) -> bool: + + return len(set(s)) == len(set(t)) == len(set(zip(s,t))) \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_40_word_pattern.py b/src/my_project/interviews/top_150_questions_round_22/ex_40_word_pattern.py new file mode 100644 index 00000000..f8a93c78 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_40_word_pattern.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def wordPattern(self, pattern: str, s: str) -> bool: + + dic_answer = dict() + + words = s.split(' ') + + if len(words) != len(pattern) or len(set(words)) != len(set(pattern)): + return False + else: + for k, v in enumerate(words): + if v in dic_answer: + if dic_answer[v] != pattern[k]: + return False + else: + dic_answer[v] = pattern[k] + + return True \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_41_valid_anagram.py b/src/my_project/interviews/top_150_questions_round_22/ex_41_valid_anagram.py new file mode 100644 index 00000000..4bb079dc --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_41_valid_anagram.py @@ -0,0 +1,13 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + + lst_s = [c for c in s] + lst_t = [c for c in t] + + lst_s.sort() + lst_t.sort() + + return lst_s == lst_t \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_38_ransom_note_round_22.py b/tests/test_150_questions_round_22/test_38_ransom_note_round_22.py new file mode 100644 index 00000000..150764ef --- /dev/null +++ b/tests/test_150_questions_round_22/test_38_ransom_note_round_22.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_38_ransom_note import Solution + +class RansomeNoteTestCase(unittest.TestCase): + + def test_is_ransome_note(self): + solution = Solution() + output = solution.canConstruct(ransomNote="aa", magazine="aab") + self.assertTrue(output) + + def test_is_no_ransome_note(self): + solution = Solution() + output = solution.canConstruct(ransomNote="a", magazine="b") + self.assertFalse(output) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_39_isomorphic_strings_round_22.py b/tests/test_150_questions_round_22/test_39_isomorphic_strings_round_22.py new file mode 100644 index 00000000..3acdd4a5 --- /dev/null +++ b/tests/test_150_questions_round_22/test_39_isomorphic_strings_round_22.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_39_isomorphic_strings import Solution + +class IsomorphicStringsTestCase(unittest.TestCase): + + def test_are_isomorphic_strings(self): + solution = Solution() + output = solution.isIsomorphic(s="egg", t="add") + self.assertTrue(output) + + def test_are_not_isomorphic_strings(self): + solution = Solution() + output = solution.isIsomorphic(s="foo", t="bar") + self.assertFalse(output) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_40_word_pattern_round_22.py b/tests/test_150_questions_round_22/test_40_word_pattern_round_22.py new file mode 100644 index 00000000..40ae8473 --- /dev/null +++ b/tests/test_150_questions_round_22/test_40_word_pattern_round_22.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_40_word_pattern import Solution + +class WordPatternTestCase(unittest.TestCase): + + def test_is_word_pattern(self): + solution = Solution() + output = solution.wordPattern(pattern="abba", s="dog cat cat dog") + self.assertTrue(output) + + def test_is_no_word_pattern_one(self): + solution = Solution() + output = solution.wordPattern(pattern="abc", s="dog cat cat fish") + self.assertFalse(output) + + def test_is_no_word_pattern_two(self): + solution = Solution() + output = solution.wordPattern(pattern="aa", s="dog cat cat fish") + self.assertFalse(output) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_41_valid_anagram_round_22.py b/tests/test_150_questions_round_22/test_41_valid_anagram_round_22.py new file mode 100644 index 00000000..47bacad2 --- /dev/null +++ b/tests/test_150_questions_round_22/test_41_valid_anagram_round_22.py @@ -0,0 +1,16 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_41_valid_anagram import Solution + +class ValidAnagramTestCase(unittest.TestCase): + + def test_is_valid_anagram(self): + solution = Solution() + output = solution.isAnagram(s="anagram", t="nagaram") + self.assertTrue(output) + + + def test_is_no_valid_anagram(self): + solution = Solution() + output = solution.isAnagram(s="rat", t="car") + self.assertFalse(output) \ No newline at end of file