注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
DoorDash obtains restaurant data from various sources which have varying quality. Thesesources often have duplicate merchants with minor typos in their names. The assignment is to
create a list of unique restaurants across various sources ignoring the errors before onboarding
them.
Definition: Similar restaurants
Two restaurants R1 and R2 are similar if we can swap a maximum of two letters (in different
positions) of R1, so that it equals R2.
For example, source one may have a restaurant named "omega grill" while another source may
have the same restaurant as "omgea grill".
For example, "biryani" and "briyani" are similar (swapping at positions 1 and 2). "biryani" is not
similar to following, "biryeni" (no e to swap with), "briynai"(Needs 2 swap)
For a given restaurant name, find and return all the similar restaurant names in the list.
Implement the function below:
public List findSimilarRestaurants(String name, String[] list) {}
#Tests
input = "hotpot"
list = ["hottop", "hotopt", "hotpit", "httoop", "hptoot"]- # Online Python compiler (interpreter) to run Python online.
- # Write Python 3 code in this online editor and run it.
- from typing import List
- def get_sign(s: str) -> str:
- cts = [0] * 26
- for c in s:
- cts[ord(c) - ord('a')] += 1
- return cts
- def find_k_anagram(s: str, candis: List[str], k: int) -> List[str]:
- anag = [candi for candi in candis if get_sign(candi) == get_sign(s)]
- result = list()
-
- for i in range(len(anag)):
- pairs = set()
- count = 0
- for cs, ci in zip(anag[i], s):
- if cs != ci:
- if (cs, ci) in pairs:
- pairs.remove((cs, ci))
- count += 1
- else:
- pairs.add((ci, cs))
- if count > k: continue
- count = count + len(pairs) - 1
- if count <= k: result.append(anag[i])
- return result
- print(find_k_anagram("hotpot", ["hottop", "hotopt", "hotpit", "httoop", "hptoot"],2))
复制代码 |