1. some questions about networking. 2. Algorithm question: We have a string (lets call it main string), its format is like following: a/b/c/... (where a,b,c could be a world and "a" is level 1, "b" is level 2 and etc. Ex: animal/pet/dog L1 L2 L3 we have a list of input strings with same structure. check which one if them is a match to the main string. we need to start checking from level 1. animal/pet/dog/white --> is a match animal/pet/cat --> is not a match pet/animal/dog/ --> is not a match animal/pet --> is not a match 3. Imagine you have the best algorithm for this problem (in term of time and memory complexity), how would you make it run even faster?
Anonymous
Javascript Solution. TimeComplexity O(n) where n is the str.length * path.length. Memory Complexity O(n) where n is the number of non path matches Memoization: Cache the paths thats have been matched so you wont need to do duplicate work e.g const cache = { "animal/pet/dog": ["animal/pet/dog/white", "animal/pet/dog/test"] } const dataPathMatch = (path, str) => { return (cache[path] && cache[path].includes(str)) || str.split(path)[0].length === 0 } const path = 'animal/pet/dog' dataPathMatch(path, 'animal/pet/dog/white') -> true dataPathMatch(path, 'animal/pet/cat') -> false dataPathMatch(path, 'pet/animal/dog/') -> false dataPathMatch(path, 'animal/pet') -> false
Check out your Company Bowl for anonymous work chats.