当前位置:网站首页>Longest substring without repeated characters (Sword finger offer 48)
Longest substring without repeated characters (Sword finger offer 48)
2022-06-27 14:22:00 【input_ out】
The longest substring without repeating characters ( The finger of the sword Offer 48)
1 Problem description
Please find the longest substring in the string that does not contain duplicate characters , Calculate the length of the longest substring .
Input : "abcabcbb"
Output : 3
explain : Because the longest substring without repeating characters is "abc", So its length is 3.
2 Their thinking
The length is N Shared string (n+1)n/2 The traversal complexity of the substring is O(n²), Judge the length as N The complexity of whether the string has duplicate characters is O(N) , Therefore, the complexity of using violence to solve this problem is O(n³).
First thought of using HashSet Reduce , Judge the length as N The complexity of whether the string has duplicate characters is O(1), towards set Add , return false There is repetition . So use double cycle plus HashSet The time complexity of solving the problem is O(n²).
thought : The first layer iterates through the string , from s[i]( Starts 0) Start , Second layer circulating feed s[i+1] Began to set Add elements to it , If returns flase, Record the current set The size is the length of the non repeating substring , And empty set Exit the second loop , use sum preservation set The maximum size . Enter the next cycle from i+1 Start judging .
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.equals("")||s==null){
return 0;
}
int sum=0;
Set<Character> set = new HashSet<>();
for(int i = 0;i<s.length();i++){
set.add(s.charAt(i));
for(int j = i+1;j<s.length();j++){
if(!set.add(s.charAt(j))){
sum=Math.max(sum,set.size());
set.clear();
break;
}
}
sum=Math.max(sum,set.size());
}
return sum;
}
}
3 improvement
The time complexity of using violence is too large , Therefore, we can consider using dynamic programming to reduce the time complexity . The main problem is to find the distance between two identical characters that are farthest apart , Consider using HashMap Save the subscript of each character , When traversing to the same character, the distance between them is saved and updated HashMap, Finally, return the maximum value of the distance saved each time .
State definition : Set up a dynamic programming list dp ,dp[j] Represents with the character s[j] For the end of “ The longest non repeating substring ” The length of .
Transfer equation : Fixed right border j , Set character s[j] The same character nearest to the left is s[i] , namely s[i] = s[i]=s[j] .
When i<0 , namely s[j] There is no same character on the left , be dp[j] = dp[j-1] + 1;
When dp[j−1]<j−i , Describe the previous and s[i] The same characters are not included in this time s[i] Substring of dp[j-1] in , be dp[j] = dp[j - 1] + 1 ;
When dp[j−1]≥j−i , Describe the previous and s[i] Want the same characters to contain s[i] Substring of dp[j−1] In the interval , be dp[j] The left boundary of is bounded by s[i] decision , namely dp[j] = j - i;
Finally back to max(dp).
Because the return value is dp List maximum , So with the help of variables tmp Storage dp[j] , Variable res Update the maximum value of each round .
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.equals("")||s==null){
return 0;
}
int temp = 0;
int res = 0;
HashMap<Character,Integer> map = new HashMap<>();
for(int i = 0;i<s.length();i++){
int j = map.getOrDefault(s.charAt(i),-1);
map.put(s.charAt(i),i);
if(temp<i-j){
temp++;
}else{
temp = i-j;
}
res = Math.max(res,temp);
}
return res;
}
}
边栏推荐
- Massive data! Second level analysis! Flink+doris build a real-time data warehouse scheme
- 【微服务|Sentinel】热点规则|授权规则|集群流控|机器列表
- Redis 主从复制、哨兵模式、Cluster集群
- 招标公告:上海市研发公共服务平台管理中心Oracle一体机软硬件维保项目
- Redis persistence
- 线程同步之信号量
- 力扣 第 81 场双周赛
- 赛迪顾问发布《“十四五” 关键应用领域之数据库市场研究报告》(附下载)
- [OS command injection] common OS command execution functions and OS command injection utilization examples and range experiments - based on DVWA range
- OpenSSF安全计划:SBOM将驱动软件供应链安全
猜你喜欢

AcWing 第57 场周赛

线程同步之信号量

国产数据库乱象

Calcul de la confidentialité Fate - Prévisions hors ligne

【业务安全-02】业务数据安全测试及商品订购数量篡改实例

CMOS level circuit analysis

Kyndryl partnered with Oracle and Veritas

Naacl 2022 | TAMT: search the transportable Bert subnet through downstream task independent mask training

Openssf security plan: SBOM will drive software supply chain security

Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance
随机推荐
A method to realize automatic renaming of pictures uploaded by WordPress
Principle Comparison and analysis of mechanical hard disk and SSD solid state disk
[advanced mathematics] from normal vector to surface integral of the second kind
American chips are hit hard again, and another chip enterprise after Intel will be overtaken by Chinese chips
Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance
基于 xml 配置文件的入门级 SSM 框架整合
NLP - monocleaner
直播app运营模式有哪几种,我们该选择什么样的模式?
注解学习总结
【业务安全03】密码找回业务安全以及接口参数账号修改实例(基于metinfov4.0平台)
AcWing 第57 场周赛
【PHP代码注入】PHP语言常见可注入函数以及PHP代码注入漏洞的利用实例
Pytorch learning 1 (learning documents on the official website)
Redis持久化
基于Vue+Node+MySQL的美食菜谱食材网站设计与实现
EventLoop learning
Design and implementation of reading app based on Web Platform
What is the difference between the FAT32 and NTFS formats on the USB flash disk
How to select cross-border e-commerce multi merchant system
Number of printouts (solved by recursive method)