2022-05-08:给你一个下标从 0 开始的字符串数组 words 。每个字符串都只包含 小写英文字母 。words 中任意一个子串中,每个字母都至多只出现一次。 如果通过以下操作之一,我们可以
2022-05-08:给你一个下标从 0 开始的字符串数组 words 。每个字符串都只包含 小写英文字母 。words 中任意一个子串中,每个字母都至多只出现一次。
如果通过以下操作之一,我们可以从 s1 的字母集合得到 s2 的字母集合,那么我们称这两个字符串为 关联的 :
往 s1 的字母集合中添加一个字母。
从 s1 的字母集合中删去一个字母。
将 s1 中的一个字母替换成另外任意一个字母(也可以替换为这个字母本身)。
数组 words 可以分为一个或者多个无交集的 组 。如果一个字符串与另一个字符串关联,那么它们应当属于同一个组。
注意,你需要确保分好组后,一个组内的任一字符串与其他组的字符串都不关联。可以证明在这个条件下,分组方案是唯一的。
请你返回一个长度为 2 的数组 ans :
ans[0] 是 words 分组后的 总组数 。
ans[1] 是字符串数目最多的组所包含的字符串数目。
输入:words = [“a”,“b”,“ab”,“cde”]。
输出:[2,3]。
解释:
- words[0] 可以得到 words[1] (将 ‘a’ 替换为 ‘b’)和 words[2] (添加 ‘b’)。所以 words[0] 与 words[1] 和 words[2] 关联。
- words[1] 可以得到 words[0] (将 ‘b’ 替换为 ‘a’)和 words[2] (添加 ‘a’)。所以 words[1] 与 words[0] 和 words[2] 关联。
- words[2] 可以得到 words[0] (删去 ‘b’)和 words[1] (删去 ‘a’)。所以 words[2] 与 words[0] 和 words[1] 关联。
- words[3] 与 words 中其他字符串都不关联。
所以,words 可以分成 2 个组 [“a”,“b”,“ab”] 和 [“cde”] 。最大的组大小为 3 。
力扣2157. 字符串分组。
答案2022-05-08:
并查集。
代码用rust编写。代码如下:
use std::collections::HashMap;
fn main() {
let words: Vec<&str> = vec!["a", "b", "ab", "cde"];
let answer = group_strings2(&words);
println!("answer = {:?}", answer);
}
fn group_strings2(words: &Vec<&str>) -> Vec<isize> {
let n = words.len() as isize;
let mut uf: UnionFind = UnionFind::new(n);
let mut strs: Vec<isize> = vec![];
for _i in 0..n {
strs.push(0);
}
let mut stands: HashMap<isize, isize> = HashMap::new();
for i in 0..n {
let mut status: isize = 0;
for c in words[i as usize].chars() {
status |= 1 << (c as u8 - 'a' as u8);
}
strs[i as usize] = status;
let mut isnone = false;
match stands.get(&status) {
Some(value) => uf.union(*value, i),
None => isnone = true,
}
if isnone {
stands.insert(status, i);
}
}
for i in 0..n {
let yes = strs[i as usize];
let no = (!yes) & ((1 << 26) - 1);
let mut tmp_yes = yes;
let mut tmp_no = no;
let mut right_one_yes;
let mut right_one_no;
// 0....0 0110011
//
// 0....0 0110011
// 0....0 0000001 -> 用
// 0....0 0110010
// 0....0 0000010 -> 用
// 0....0 0110000
while tmp_yes != 0 {
right_one_yes = tmp_yes & (-tmp_yes);
let isok: bool;
match stands.get(&(yes ^ right_one_yes)) {
Some(_value) => isok = true,
None => isok = false,
}
if isok {
uf.union(i, *stands.get(&(yes ^ right_one_yes)).unwrap());
}
tmp_yes ^= right_one_yes;
}
// tmpNo = 该去试试什么添加!
while tmp_no != 0 {
right_one_no = tmp_no & (-tmp_no);
let isok: bool;
match stands.get(&(yes | right_one_no)) {
Some(_value) => isok = true,
None => isok = false,
}
if isok {
uf.union(i, *stands.get(&(yes | right_one_no)).unwrap());
}
tmp_no ^= right_one_no;
}
tmp_yes = yes;
while tmp_yes != 0 {
right_one_yes = tmp_yes & (-tmp_yes);
tmp_no = no;
while tmp_no != 0 {
right_one_no = tmp_no & (-tmp_no);
let isok: bool;
match stands.get(&((yes ^ right_one_yes) | right_one_no)) {
Some(_value) => isok = true,
None => isok = false,
}
if isok {
uf.union(
i,
*stands.get(&((yes ^ right_one_yes) | right_one_no)).unwrap(),
);
}
tmp_no ^= right_one_no;
}
tmp_yes ^= right_one_yes;
}
}
let ans: Vec<isize> = vec![uf.sets(), uf.max_size()];
return ans;
}
pub struct UnionFind {
pub parent: Vec<isize>,
pub size: Vec<isize>,
pub help: Vec<isize>,
}
impl UnionFind {
pub fn new(n: isize) -> UnionFind {
let mut parent: Vec<isize> = vec![];
let mut size: Vec<isize> = vec![];
let mut help: Vec<isize> = vec![];
for i in 0..n {
parent.push(i);
size.push(1);
help.push(0);
}
UnionFind {
parent: parent,
size: size,
help: help,
}
}
pub fn find(&mut self, i: isize) -> isize {
let mut i = i;
let mut hi: isize = 0;
while i != self.parent[i as usize] {
self.help[hi as usize] = i;
hi += 1;
i = self.parent[i as usize];
}
hi -= 1;
while hi >= 0 {
self.parent[self.help[hi as usize] as usize] = i;
hi -= 1;
}
return i;
}
pub fn union(&mut self, i: isize, j: isize) {
let f1 = self.find(i);
let f2 = self.find(j);
if f1 != f2 {
if self.size[f1 as usize] >= self.size[f2 as usize] {
self.size[f1 as usize] += self.size[f2 as usize];
self.parent[f2 as usize] = f1;
} else {
self.size[f2 as usize] += self.size[f1 as usize];
self.parent[f1 as usize] = f2;
}
}
}
pub fn sets(&mut self) -> isize {
let mut ans: isize = 0;
for i in 0..self.parent.len() {
ans += if self.parent[i] == i as isize { 1 } else { 0 };
}
return ans;
}
pub fn max_size(&mut self) -> isize {
let mut ans: isize = 0;
for i in 0..self.size.len() {
ans = if ans > self.size[i as usize] {
ans
} else {
self.size[i as usize]
};
}
return ans;
}
}
执行结果如下:
2022-05-08:给你一个下标从 0 开始的字符串数组 words 。每个字符串都只包含 小写英文字母 。words 中任意一个子串中,每个字母都至多只出现一次。 如果通过以下操作之一,我们可以的更多相关文章
- 给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
给定两个字符串 s 和 t,它们只包含小写字母.字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母.请找出在 t 中被添加的字母. 示例: 输入: s = "abcd" ...
- 获取页面中任意一个元素距离body的偏移量
//offSet:等同于jQuery中的offSet方法,获取页面中任意一个元素距离body的偏移量function offSet(curEle) { var totalLeft = null; va ...
- 【剑指offer】找出数组中任意一个重复的数字,C++实现
原创博文,转载请注明出处! # 题目 # 思路 对于长度为n的数组,范围为0~n-1的数字而言,如果不粗在重复数字,则排序后数组元素和数组角标相同.如果存在重复数字,则在排序的过程中会出现不同下标对应 ...
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- [Robot Framework] 校验字符串中是否包含某个子字符串,校验同时满足两个条件中任意一个
${tWarningMessage} Run Keyword If ${tIfExist} AutoItLibrary.Win Get Text Generate Fee Data warning m ...
- java 正则表达式 验证字符串 只包含汉字英文数字
String content = “testContent”; String regex="^[a-zA-Z0-9\u4E00-\u9FA5]+$"; Pattern patter ...
- 让android系统中任意一个view变成进度条
1.效果 2.进度条背景drawable文件 结束后可以恢复原背景. <?xml version="1.0" encoding="utf-8"?> ...
- VIM 用正则表达式,非贪婪匹配,匹配竖杠,竖线, 匹配中文,中文正则,倒数第二列, 匹配任意一个字符 :
VIM 用正则表达式 批量替换文本,多行删除,复制,移动 在VIM中 用正则表达式 批量替换文本,多行删除,复制,移动 :n1,n2 m n3 移动n1-n2行(包括n1,n2)到n3行之下: ...
- 只包含schema的dll生成和引用方法
工作中,所有的tools里有一个project是只包含若干个schema的工程,研究了一下,发现创建这种只包含schema的dll其实非常简单. 首先,在visual studio-new proje ...
- 面试题:编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。(c++实现)
实例说明 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ...
随机推荐
- selenium 模拟鼠标滚轮,滚动到可见的选项
self.wrap_driver.move_to_element(locator=const_xpath.monitor_select) #鼠标移动到某个区域target = self.driver. ...
- pyecharts 学习使用网址
pyecharts新版官方手册地址:https://pyecharts.org/#/zh-cn/intro 或http://pyecharts.org/#/?id=pyecharts或http://p ...
- Python学习笔记--循环的知识以及应用
while循环 代码: 结果: 案例:求1-100的和 实现: 案例:while循环猜数字 实现: while循环的嵌套使用 案例:打印九九乘法表 (注意:要是想要输出不换行,代码可以这样写:prin ...
- 后疫情时代,RTE“沉浸式”体验还能这么玩?丨RTE 2022 编程挑战赛赛后专访
前言 9 月 17 日,由声网.环信与 RTE 开发者社区联合主办的"RTE 2022 编程挑战赛"圆满落幕.从 300+ 支参赛队伍中冲出重围的 27 支决赛队伍,在元宇宙中用精 ...
- springboot实现短信验证码的发送
我使用的是阿里云短信服务 代码前的准备 1. 申请阿里云的短信服务 2. 添加签名,这里需要等待审核通过 3. 在模板管理设置自己的短信模板 下面添加模板,选择验证码,模板内容可以直接使用输入框内的示 ...
- Duplicate File Finder Pro - 重复文件查找器,给你的 Mac 清理出大量磁盘空间
重复文件查找器 Duplicate File Finder Pro 是一个实用程序,只需3次点击就能在Mac上找到重复的文件.拖放功能和尽可能多的文件夹,你想,然后按下扫描按钮.在一分钟,应用程序将给 ...
- Laf v1.0 发布:函数计算只有两种,30s 放弃的和 30s 上线的
一般情况下,开发一个系统都需要前端和后端,仅靠一个人几乎无法胜任,需要考虑的特性和功能非常多,比如: 需要一个数据库来存放数据: 需要一个文件存储来存放各种文件,比如图片文件: 后端需要提供接口供前端 ...
- window计时器函数
// 定时器: // 计时器 // 开启:setInterval() // 参数1:回调函数 // 参数2:毫秒数 // 功能:每个指定的毫秒数执行一次回调函数 demo: var t = setIn ...
- 用BingGPT写一首勉励自己的诗
觉得写的还挺有意思,所以记录一下,祝自己在今后的生活中努力学习,学有所成 勤学不辍志,博览群书知. 海纳百川理,山高自有路. 勿以时日长,惟以功夫深.
- .NET周报 【4月第1期 2023-04-02】
国内文章 探索 SK 示例 -- GitHub 存储库中的机器人 https://www.cnblogs.com/shanyou/p/17280627.html 微软 3月22日 一篇文章" ...