Skip to content

Commit b39f74c

Browse files
authored
Merge pull request #1482 from ivan1016017/december11
adding algo
2 parents f42e7c6 + 3849079 commit b39f74c

4 files changed

Lines changed: 69 additions & 0 deletions

File tree

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+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if it is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
if s[i] != s[len_s - 1 -i]:
19+
return False
20+
21+
return True
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def isSubsequence(self, s: str, t: str) -> bool:
6+
7+
l1, l2 = 0, 0
8+
9+
len_s, len_t = len(s), len(t)
10+
11+
while l1 < len_s and l2 < len_t:
12+
13+
if s[l1] == t[l2]:
14+
l1 += 1
15+
16+
l2 += 1
17+
18+
return l1 == len_s
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_25_valid_palindrome import Solution
4+
5+
class ValidPalindromeTestCase(unittest.TestCase):
6+
7+
def test_is_valid_palindrome(self):
8+
solution = Solution()
9+
output = solution.isPalindrome(s="A man, a plan, a canal: Panama")
10+
self.assertTrue(output)
11+
12+
def test_is_no_valid_palindrome(self):
13+
solution = Solution()
14+
output = solution.isPalindrome(s="race a car")
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_26_is_subsequence import Solution
4+
5+
class IsSubsequenceTestCase(unittest.TestCase):
6+
7+
def test_is_subsequence(self):
8+
solution = Solution()
9+
output = solution.isSubsequence(s="abc", t="ahbgdc")
10+
self.assertTrue(output)
11+
12+
def test_is_no_subsequence(self):
13+
solution = Solution()
14+
output = solution.isSubsequence(s="axc", t="ahbgdc")
15+
self.assertFalse(output)

0 commit comments

Comments
 (0)