Skip to content

Commit 4038fc0

Browse files
committed
adding algo
1 parent 93c6c5f commit 4038fc0

3 files changed

Lines changed: 52 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='[^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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def strStr(self, haystack: str, needle: str) -> int:
6+
7+
len_needle = len(needle)
8+
len_haystack = len(haystack)
9+
10+
for i in range(len_haystack - len_needle + 1):
11+
if haystack[i:i+len_needle] == needle:
12+
return i
13+
14+
return -1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_21\
3+
.find_index_first_occurrence import Solution
4+
5+
class FirstOccurrenceIndex(unittest.TestCase):
6+
7+
def test_first_occurrence_index(self):
8+
solution = Solution()
9+
output = solution.strStr(haystack="sadbutsad", needle="sad")
10+
target = 0
11+
self.assertEqual(output, target)
12+
13+
def test_no_first_occurrence_index(self):
14+
solution = Solution()
15+
output = solution.strStr(haystack="leetcode", needle="leeto")
16+
target = -1
17+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)