

Implement strStr().
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
Input: haystack = "hello", needle = "ll"
Output: 2Input: haystack = "aaaaa", needle = "bba"
Output: -11 <= haystack.length, needle.length <= 104haystack and needle consist of only lowercase English characters.class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        
        # if needle is empty
        if needle=="": return 0
        _len=len(needle)      
        for i in range(len(haystack)-_len+1):
            # if needle is part of the haystack
            if haystack[i:i+_len]==needle: return i
        # if needle is not part of the haystack  
        return -1Time Complexity: O(n*h)
The string comparison is O(n) where n is size of needle so solution is O(n*h) at best where h is size of haystack.
When you slice the string like haystack here for example, you're creating a copy of the string.
시작인덱스:종료인덱스:step]
종료인덱스의 원소는 포함되지 않고 바로 앞 원소까지만 포함된다.
list, set, dictlist의 얕은 복사의 경우,b 에 a를 할당하면 값이 할당되는 것이 아니라 같은 메모리 주소를 바라본다.b를 변경하면 같이 a도 바뀐다.bool. int, float, tuple, str, frozensetstr의 얕은 복사의 경우,b 에 a를 할당하면 값이 할당되는 것이 아니라 같은 메모리 주소를 바라본다.b에 다른 값을 할당하면 재할당이 이루어지며 메모리 주소가 변경된다.a와 b는 다른 값을 가진다.a="hello"
b=a
# a=hello
# b=hello
print(a is b)
> True
# b에 다른 값을 할당하는 경우
b="python"
# a=hello
# b=python
print(a is b)
> Falsea = [1,2,3]
b = a[:]
print(id(a)) 
# 4396179528
print(id(b))
# 4393788808위처럼 리스트 a와 리스트 b의 id는 다르다. 
>>> a == b
True
>>> a is b
False따라서 위와 같은 결과가 나온다.
l = [0, 1, [2, 3]]
l_whole_slice = l[:]id(l) 과 id(l_whole_slice) 값은 다르지만, 그 내부의 객체 id(l[0]) 과 id(l_whole_slice[0])은 같은 주소를 바라보고 있다.l[1] 값을 변경하면 l_whole_slice[1]도 따라 변경된다.l[1] = 100
l[2][0] = 200
print(l)
# [0, 100, [200, 3]]
print(l_whole_slice)
# [0, 1, [200, 3]]copy 모듈의 copy() methodcopy() methodlist(), dict(), etccopy 모듈의 deepcopy() method는 깊은 복사(deep copy)에 해당한다.l = [0, 1, [2, 3]]
l_deepcopy = copy.deepcopy(l)
print(l is l_deepcopy)
# False
print(l[2] is l_deepcopy[2])
# False
l[1] = 100
l[2][0] = 200
print(l)
# [0, 100, [200, 3]]
print(l_deepcopy)
# [0, 1, [2, 3]]References