Skip to content

Commit 096dce2

Browse files
authored
Merge pull request #1512 from ivan1016017/january10
adding algo
2 parents 71fdf96 + 587d625 commit 096dce2

8 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def canConstruct(self,ransomNote: str, magazine: str) -> bool:
6+
7+
for c in set(ransomNote):
8+
if ransomNote.count(c) > magazine.count(c):
9+
return False
10+
11+
return True
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def isIsomorphic(self, s: str, t: str) -> bool:
6+
7+
return len(set(s)) == len(set(t)) == len(set(zip(s,t)))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def wordPattern(self, pattern: str, s: str) -> bool:
6+
7+
dic_answer = dict()
8+
9+
words = s.split(' ')
10+
11+
if len(words) != len(pattern) or len(set(words)) != len(set(pattern)):
12+
return False
13+
else:
14+
for k, v in enumerate(words):
15+
if v in dic_answer:
16+
if dic_answer[v] != pattern[k]:
17+
return False
18+
else:
19+
dic_answer[v] = pattern[k]
20+
21+
return True
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def isAnagram(self, s: str, t: str) -> bool:
6+
7+
lst_s = [c for c in s]
8+
lst_t = [c for c in t]
9+
10+
lst_s.sort()
11+
lst_t.sort()
12+
13+
return lst_s == lst_t
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_38_ransom_note import Solution
4+
5+
class RansomeNoteTestCase(unittest.TestCase):
6+
7+
def test_is_ransome_note(self):
8+
solution = Solution()
9+
output = solution.canConstruct(ransomNote="aa", magazine="aab")
10+
self.assertTrue(output)
11+
12+
def test_is_no_ransome_note(self):
13+
solution = Solution()
14+
output = solution.canConstruct(ransomNote="a", magazine="b")
15+
self.assertFalse(output)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_39_isomorphic_strings import Solution
4+
5+
class IsomorphicStringsTestCase(unittest.TestCase):
6+
7+
def test_are_isomorphic_strings(self):
8+
solution = Solution()
9+
output = solution.isIsomorphic(s="egg", t="add")
10+
self.assertTrue(output)
11+
12+
def test_are_not_isomorphic_strings(self):
13+
solution = Solution()
14+
output = solution.isIsomorphic(s="foo", t="bar")
15+
self.assertFalse(output)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_40_word_pattern import Solution
4+
5+
class WordPatternTestCase(unittest.TestCase):
6+
7+
def test_is_word_pattern(self):
8+
solution = Solution()
9+
output = solution.wordPattern(pattern="abba", s="dog cat cat dog")
10+
self.assertTrue(output)
11+
12+
def test_is_no_word_pattern_one(self):
13+
solution = Solution()
14+
output = solution.wordPattern(pattern="abc", s="dog cat cat fish")
15+
self.assertFalse(output)
16+
17+
def test_is_no_word_pattern_two(self):
18+
solution = Solution()
19+
output = solution.wordPattern(pattern="aa", s="dog cat cat fish")
20+
self.assertFalse(output)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_41_valid_anagram import Solution
4+
5+
class ValidAnagramTestCase(unittest.TestCase):
6+
7+
def test_is_valid_anagram(self):
8+
solution = Solution()
9+
output = solution.isAnagram(s="anagram", t="nagaram")
10+
self.assertTrue(output)
11+
12+
13+
def test_is_no_valid_anagram(self):
14+
solution = Solution()
15+
output = solution.isAnagram(s="rat", t="car")
16+
self.assertFalse(output)

0 commit comments

Comments
 (0)