当前位置:网站首页>1542. Find the longest super substring hash + state compression
1542. Find the longest super substring hash + state compression
2022-07-27 18:13:00 【Empress Yu】
1542. Find the longest favorite substring
Give you a string
s. Please returnsThe longest of all Super substring The length of .「 Super substring 」 The following two conditions must be met :
- The string is
sA non empty substring of- After any number of character exchanges , This string can become a palindrome string
Example 1:
Input :s = "3242415" Output :5 explain :"24241" Is the longest super substring , After exchanging the characters , You can get palindromes "24142"Example 2:
Input :s = "12345678" Output :1Example 3:
Input :s = "213123" Output :6 explain :"213123" Is the longest super substring , After exchanging the characters , You can get palindromes "231132"Example 4:
Input :s = "00" Output :2Tips :
1 <= s.length <= 10^5sIt's just numberssource : Power button (LeetCode)
link :https://leetcode.cn/problems/find-longest-awesome-substring
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The result of doing the question
success , Thanks to the construction problem of spirit and God , This kind of question gradually has some ideas
Method : Hash + State compression
One particular thing to note is , Can a palindrome string be formed , There are actually two situations , One is the odd length and the other is the even length
1. The length is even : The length of each character is even
2. The length is odd : One character in the middle , The length is odd , Everything else is even
Then we have an important direction : Pay attention to the parity of strings .
1. The previous string can find the same parity as the current string , Then the two segments are subtracted , Get that every character is even , Subtract to find the even string ;
2. The parity formed by the previous string , It's one bit worse than the current situation , We get the current parity , Reverse one of them , You can find the string with odd length
3. Because we want the string to be as long as possible , So find someone for the first time key after , No subsequent replacement
4. Parity can be computed by bits 1 and 0 To mark , Find parity by prefix
class Solution {
public int longestAwesome(String s) {
long v = 0;
Map<Long,Integer> map = new HashMap<>();// Parity -> Location
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;
}
}边栏推荐
猜你喜欢
![[user article] examples of P4 consolidation practice guide disassemble resolve](/img/84/503fc057ce642038f693b38be69bc0.png)
[user article] examples of P4 consolidation practice guide disassemble resolve

vue使用keep-alive实现页面缓存
![[Southwest University] information sharing of postgraduate entrance examination and re examination](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Southwest University] information sharing of postgraduate entrance examination and re examination

Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks

美团二面:为什么Redis会有哨兵?

查看端口PID及结束进程

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

快速获取网站媒体资源方法

Convolutional neural network -- Introduction to FPN (feature pyramid networks)

Salesforce Dynamic Forms
随机推荐
Golang Chan implements mutual exclusion
防止sql注入
最新大厂高级面试题 必备
【学习笔记】Redis中有序集合zset的实现原理——跳表
1542. 找出最长的超赞子字符串 哈希+状态压缩
给程序界面增加音乐,加载背景照片。
Exciting collection of new features released by salesforce
年终总结模板
PostgreSQL 14 支持winserver2022吗?
《华为是谁》纪录短片集登陆BBC:曝光大量任正非不为人知经历
Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks
MySql代码数据库创建 停车管理系统 外键
国巨斥资18亿美元收购竞争对手Kemet,交易或在明年下半年完成
[Southwest University] information sharing of postgraduate entrance examination and re examination
查找表中多余重复记录并删除保留最小一个
知物由学 | APP大瘦身,新一代AAB框架下的安全加固之道
IDEA打包war包与war包位置
[learning notes] advanced version of MySQL database - index optimization, slow query, locking mechanism, etc
You can't specify target table 'table name' for update in from clause error resolution in MySQL
7月第4周易盾业务风控关注 | 最高法对APP强索个人信息进行规制