查看: 1162| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[高频题] 刀大师经典题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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"]
  1. # Online Python compiler (interpreter) to run Python online.
  2. # Write Python 3 code in this online editor and run it.
  3. from typing import List   
  4. def get_sign(s: str) -> str:
  5.     cts = [0] * 26
  6.     for c in s:
  7.         cts[ord(c) - ord('a')] += 1
  8.     return cts

  9. def find_k_anagram(s: str, candis: List[str], k: int) -> List[str]:
  10.     anag = [candi for candi in candis if get_sign(candi) == get_sign(s)]
  11.     result = list()
  12.    
  13.     for i in range(len(anag)):
  14.         pairs = set()
  15.         count = 0
  16.         for cs, ci in zip(anag[i], s):
  17.             if cs != ci:
  18.                 if (cs, ci) in pairs:
  19.                     pairs.remove((cs, ci))
  20.                     count += 1
  21.                 else:
  22.                     pairs.add((ci, cs))
  23.         if count > k: continue
  24.         count = count + len(pairs) - 1
  25.         if count <= k: result.append(anag[i])
  26.     return result            

  27. print(find_k_anagram("hotpot", ["hottop", "hotopt", "hotpit", "httoop", "hptoot"],2))
复制代码

评分

参与人数 1大米 +6 收起 理由
14417335 + 6 给你点个赞!

查看全部评分


上一篇:分享最新高频题: 卖他 狗 和 巨硬
下一篇:刀大师面经题时间间隔
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表