Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/3b447bb0fb12ffb4c8fd72d7b79c182c to your computer and use it in GitHub Desktop.
Save jianminchen/3b447bb0fb12ffb4c8fd72d7b79c182c to your computer and use it in GitHub Desktop.
Find smallest substring containing all characters - Julia reviewed the code, gave out brute force solution, major hint to write brute force solution. Python code
def get_shortest_unique_substring(arr, str):
# if either arr or string is empty, return ''
# iterate through the array, for each item
# see if that item exists in the string
# if so, store it in a dict, with the letter as the key and the count as the value
substrings = []
start_idx = 0
while start_idx < len(str) - 1:
end_idx = start_idx
for end_idx < len(str) - 1:
substring = str[start_idx: end_idx]
substrings.append(substring)
start_idx += 1
output_str = ''
for substring in substrings:
found = true // *
for letter in array:
if not substring.contains(letter):
found = false
break
else:
//if len(substring) < output_string: // one letter - make containing all letters -> **
// output_str = substring // ***
// find all letters
if(found) // ****
substring containing -> // *****
return output
* is the line of code added or commented out by Julia
Julia gave out major hint to write brute force solution after 10 minutes in the following:
for(start_idx = 0; start_idx < len(str); start_idx++)
{
for(end_idx = start_idx; end_idx < len(str);end_idx++)
{
substring = str[start_idx, end_idx]
}
}
Julia gave out the brute force solution analysis first in the following after 10 minutes mock interview:
[z,y,x]
xyyzyzyx
-> brute force
yyzy -> z, y, x no
yyzyzyx -> z, y, x, yes -> 7, what is minimum?
Find all possible solution
Let us say that string length is n, what is possible substring in total?
start position O(n)
end position O(n)
what is substring option? O(n * n)
what is time complexity to find keys in substring? linear, O(n)
O(n * n * n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment