Skip to content

Commit 818c917

Browse files
authored
Merge pull request #1302 from ivan1016017/june14
adding algo
2 parents c7c645f + 74d4395 commit 818c917

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import re
2+
3+
class Solution:
4+
def isPalindrome(self, s: str) -> bool:
5+
6+
# to lowercase
7+
s = s.lower()
8+
9+
# remove non-alphanumeric characters
10+
s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s)
11+
12+
# Validate palindrome
13+
len_s = len(s)
14+
15+
for i in range(len_s//2):
16+
17+
if s[i] != s[len_s - 1 - i]:
18+
return False
19+
20+
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_17\
3+
.finding_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)