Last active
December 15, 2021 12:17
-
-
Save brianspiering/dcb8cdcf1c45d23731d684d2e8326f28 to your computer and use it in GitHub Desktop.
Is Substring Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution for Is A Substring Problem | |
def is_substring(short:str, long:str) -> bool: | |
for i, c in enumerate(long): | |
if c == short[0]: | |
if long[i:i+len(short)] == short: | |
return True | |
else: | |
return False | |
assert is_substring(short="lo", long="Hello, world!") # Short in long | |
assert not is_substring(short= "🦄", long="Hello, world!") # Not (short in long) | |
""" | |
Notes to interviewer | |
------- | |
Common Questions | |
----- | |
- Q: Can I use built-in string methods? | |
- A: No | |
- Q: Can I use `in`? | |
- A: No | |
- Q: Should I care about case? | |
- A: Yes - It needs to be an exact character match | |
- Q: Can the strings be empty? | |
- A: Assume both strings are not empty | |
Big 0 | |
----- | |
Time: O(n) / linear - where n is the length of the longer string | |
Space: 0(1) / constant - takes no more space | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment