当前位置:网站首页>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;
}
}边栏推荐
- The latest advanced interview questions for big factories are necessary
- 卷积神经网络——从R-CNN,Fast R-CNN到Faster R-CNN,Mask R-CNN
- 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
- 【cf】#681 A. Kids Seating (Div. 2, based on VK Cup 2019-2020 - Final)
- 7月第4周易盾业务风控关注 | 最高法对APP强索个人信息进行规制
- Bug records using distributed framework WCF
- With the right tools, CI achieves twice the result with half the effort
- Behind every piece of information you collect, you can't live without TA
- [introduction to database system (Wang Shan)] Chapter 4 - Database Security
- How to develop an online Excel spreadsheet system (Part 1)
猜你喜欢

卷积神经网络——从R-CNN,Fast R-CNN到Faster R-CNN,Mask R-CNN

【cf】#681 A. Kids Seating (Div. 2, based on VK Cup 2019-2020 - Final)

登录页面tableLayout(表格布局)

JSP自定义标签(下)

Evaluation index of machine learning (II) -- classification evaluation index

Learn from things | Yidun mobile terminal isomorphism practice, improve the official website interaction experience in a few steps

Personal understanding of convolution calculation process of convolution neural network

EF框架简介

Convolutional neural network -- Translation of yolov1 thesis
How to improve the security of Android applications?
随机推荐
卷积神经网络之卷积计算过程个人理解
The Ministry of industry and information technology re governs data security, and Netease Yidun "privacy compliance" keeps the bottom line of enterprise operation
Wechat applet to make calls
Hutool string utility class
卷积神经网络——从R-CNN,Fast R-CNN到Faster R-CNN,Mask R-CNN
查找表中多余重复记录并删除保留最小一个
微信小程序 实现位置地图显示,引入map地图,不含导航
Hutool digital computing
卷积神经网络——YOLOV1论文翻译
hutool- 数组工具
[Southwest University] information sharing of postgraduate entrance examination and re examination
With the right tools, CI achieves twice the result with half the effort
微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists
Es query limit 10000 data solutions
CPU introduction
2022 safety officer-a certificate examination questions and online simulation examination
I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
Wechat applet realizes location map display and introduces map map without navigation
WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executabl
卷积神经网络——YOLOV2(YOLO9000)论文翻译