当前位置:网站首页>1542. 找出最长的超赞子字符串 哈希+状态压缩
1542. 找出最长的超赞子字符串 哈希+状态压缩
2022-07-27 15:57:00 【钰娘娘】
1542. 找出最长的超赞子字符串
给你一个字符串
s。请返回s中最长的 超赞子字符串 的长度。「超赞子字符串」需满足满足下述两个条件:
- 该字符串是
s的一个非空子字符串- 进行任意次数的字符交换后,该字符串可以变成一个回文字符串
示例 1:
输入:s = "3242415" 输出:5 解释:"24241" 是最长的超赞子字符串,交换其中的字符后,可以得到回文 "24142"示例 2:
输入:s = "12345678" 输出:1示例 3:
输入:s = "213123" 输出:6 解释:"213123" 是最长的超赞子字符串,交换其中的字符后,可以得到回文 "231132"示例 4:
输入:s = "00" 输出:2提示:
1 <= s.length <= 10^5s仅由数字组成来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-longest-awesome-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
做题结果
成功,这要感谢灵神的构造题,这种题渐渐的也有点思路了
方法:哈希+状态压缩
特别需要注意的一点是,能否形成回文串,实际上是有两种情况,一种是长度奇数一种是长度偶数
1. 长度是偶数:那每个字符的长度都是偶数
2. 长度是奇数:中间的一个字符,长度是奇数,其他都是偶数
那我们就有个重要的方向:关注字符串的奇偶性。
1. 前面的字符串能找到和当前字符串奇偶性相同的情况,则两段相减,得到每个字符都是偶数,相减就可以找到长度偶数串;
2. 前面的字符串形成的奇偶性,和当前差一位的情况,我们拿到当前的奇偶性,取反其中的一位,就可以找到长度为奇数的字符串
3. 由于我们希望字符串尽可能的长,所以首次找到某个 key 之后,后续不再替换
4. 奇偶性可用位运算的 1 和 0 来标识,前缀的方式求奇偶性
class Solution {
public int longestAwesome(String s) {
long v = 0;
Map<Long,Integer> map = new HashMap<>();//奇偶性->位置
map.put(v,-1);
int n = s.length();
int ans = 0;
for(int i =0 ; i < n; i++){
int index = s.charAt(i)-'0';
v = v ^ ((long)1<<index);
ans = Math.max(ans,i-map.getOrDefault(v,i));
for(int j = 0; j < 10; j++){
long diff = (v ^ ((long)1<<j));
ans = Math.max(ans,i-map.getOrDefault(diff,i));
}
if(!map.containsKey(v)) map.put(v,i);
}
return ans;
}
}边栏推荐
- [Southwest University] information sharing of postgraduate entrance examination and re examination
- VSS tip: search all checked out files (search checked out files according to users)
- Detailed explanation of browser caching mechanism
- Kubernetes 1.24 high availability cluster binary deployment
- 备份表恢复表
- 注释中的{@code}、{@Link} 与<p>等标签
- How to develop an online Excel spreadsheet system (Part 1)
- MySQL creates a student table and queries grades
- 工信部再治数据安全,网易易盾“隐私合规”守住企业经营底线
- mysql解决唯一索引重复导致的插入失败问题
猜你喜欢

Salesforce certified sharing and visibility Designer (su20) certification examination summary

VSS tip: search all checked out files (search checked out files according to users)

2022 high altitude installation, maintenance and removal of test question simulation test platform operation

XStream reports an error abstractreflectionconverter$unknownfield exception when parsing XML

vue使用keep-alive实现页面缓存

Machine learning: IOU of concept understanding

WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executabl

Are those who are absent from the written examination shortlisted for the teacher recruitment interview? Henan Xiangfu: the statistics of individual candidates' scores are wrong

The latest advanced interview questions for big factories are necessary

7月第4周易盾业务风控关注 | 最高法对APP强索个人信息进行规制
随机推荐
WPF做登陆界面
卷积神经网络——FPN(Feature Pyramid Networks)介绍
How to resolve the public domain name to the intranet IP server -- quickly resolve the domain name and map the Internet access
Compilation and testing of raspberry pie driver code
Salesforce certified sharing and visibility Designer (su20) certification examination summary
[introduction to database system (Wang Shan)] Chapter 5 - database integrity
EF框架简介
Bubble sorting in JS
Original direct selling MOS tube knl42150 2.8a/1500v applicable photovoltaic inverter can provide samples
I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
hutool 字符串工具类
机器学习之评价指标(一)——回归评价指标
年终总结模板
Convolutional neural network -- SSD thesis translation
注释中的{@code}、{@Link} 与<p>等标签
shell常见命令(1)——变量大小写转换
机器学习之评价指标(二)——分类评价指标
vue使用keep-alive实现页面缓存
【Codeforces】 B. Make it Divisible by 25
[introduction to database system (Wang Shan)] Chapter 11 concurrency control