Skip to content

Commit 89062e9

Browse files
authored
Merge pull request #1372 from ivan1016017/august23
adding algo
2 parents 5409b3a + 35d4754 commit 89062e9

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
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_19\
3+
.finding_first_occurence_index 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)