当前位置:网站首页>leetcode:6094. Company name [group enumeration + cannot repeat set intersection + product Cartesian product (repeat indicates length)]
leetcode:6094. Company name [group enumeration + cannot repeat set intersection + product Cartesian product (repeat indicates length)]
2022-06-12 18:43:00 【Review of the white speed Dragon King】

analysis
Start with the first letter key, The following letter is value( To get a set Come out and save )
Then different initials are divided into different groups
And then through product The cartesian product , Consider different groups , Only consider the case that the first letter selected is less than the second letter ( The opposite can happen *2 Handle , The same does not conform to )
Then there are two of these two groups set, If set There is the same in , Obviously, this value cannot be obtained , Otherwise, it will be exchanged and get in another set in
So the contribution of the two groups at this time is (len(s1) - len(s1 & s2)) * (len(s2) - len(s1 & s2)) * 2
Be careful py Medium set It's disorder hashtable It's done , So all the additions, deletions, modifications and checks are o1, So generally speaking, this sentence is spelled as on Complexity
So the total complexity is 26 ** 2 * on complacent
ac code
class Solution:
def distinctNames(self, ideas: List[str]) -> int:
d = defaultdict(set)
for name in ideas: d[name[0]].add(name[1:])
print(d)
print(list(product(d, repeat = 2))) # The length is 2 The Cartesian product of
#return sum((len(d[a]) - len(d[a] & d[b])) * (len(d[b]) - len(d[a] & d[b])) * 2 for a, b in product(d, repeat=2) if ord(a) < ord(b))
ans = 0
for a, b in product(d, repeat=2): # 26 * 26
if ord(a) < ord(b): # in order and * 2
#print(d[a])
#print(d[b])
#print(d[a] & d[b])
# set -> hashtable, & -> o(n)
ans += (len(d[a]) - len(d[a] & d[b])) * (len(d[b]) - len(d[a] & d[b])) * 2
return ans
summary
Split the title
Group according to the first letter
Different groups of words must not be followed by the same
Then remember to take 2 that will do ( In this way, we only need to consider the case that the former is less than the latter )
边栏推荐
- MySQL advanced learning notes
- 常用问题排查工具和分析神器,值得收藏
- The difference between user status and system status in CRM
- 232-CH579M学习开发-以太网例程-TCP服务器(项目应用封装,局域网或广域网测试)
- A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
- Hash hash
- Hugo 博客搭建教程
- 观察网站的页面
- 基于STM32设计智能家居控制系统(OneNet)_2022
- Leetcode 416. Split equal sum subset
猜你喜欢

Review of MySQL (VI): usage of Union and limit

MYSQL:Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column

How to download proxystrike in China

Mysql ->>符号用法 Json相关

How to modify the authorization of sina Weibo for other applications

基于Halcon的螺栓螺丝部分划痕、腐蚀缺陷检测

MySQL - > > symbol usage JSON related
![[blockbuster release] ant dynamic card, enabling the app home page to realize agile update](/img/65/5ed80090f4d0ee92b01888eb496528.jpg)
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update

国内如何下载ProxyStrike

Topic 66: input array, exchange the largest element with the first element, exchange the smallest element with the last element, and output array.
随机推荐
Gd32f4xx communicates with electric energy meter conforming to dlt645_ two
Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
Can tonghuashun open an account? Can tonghuashun directly open the security of securities companies on the app
基于Halcon的矩形卡片【手动绘制ROI】的自由测量
Two months later, my second listing anniversary [June 2, 2022]
309. the best time to buy and sell stocks includes the freezing period
Basic SQL statement - select (single table query)
What is SAP support package stack
Extracting strings with grep awk
Adjust CEPH cluster image source
Hugo 博客搭建教程
Enhanced version of unit test code displayed by SAP e-commerce cloud Spartacus UI checkout spinner
CVPR 2022 Oral 大连理工提出SCI:快速、超强的低光照图像增强方法
看不懂Kotlin源码?从Contracts 函数说起~
Leetcode topic [string]-541- reverse string II
Review of MySQL (I): go deep into MySQL
Vue —— 进阶 vue-router 路由(二)(replace属性、编程式路由导航、缓存路由组件、路由的专属钩子)
在思科模拟器Cisco Packet Tracer实现自反ACL
实验10 Bezier曲线生成-实验提高-交互式生成B样条曲线
A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud