当前位置:网站首页>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;
}
}
边栏推荐
- Sword finger offer II 039 Histogram maximum rectangular area monotonic stack
- 机械硬盘和ssd固态硬盘的原理对比分析
- 外部存储器
- Interpretation of new version features of PostgreSQL 15 (including live Q & A and PPT data summary)
- Naacl 2022 | TAMT: search the transportable Bert subnet through downstream task independent mask training
- Shell concise tutorial
- NLP - monocleaner
- Four characteristics of transactions
- 【每日3题(3)】盒子中小球的最大数量
- Privacy computing fat offline prediction
猜你喜欢
CMOS级电路分析
国产数据库乱象
[XMAN2018排位赛]通行证
基于SSM的Web网页聊天室系统
Completely solve the problem of Chinese garbled code in Web Engineering at one time
Kyndryl partnered with Oracle and Veritas
Deep understanding of bit operations
Summary and Thinking on interface test automation
American chips are hit hard again, and another chip enterprise after Intel will be overtaken by Chinese chips
Array related knowledge
随机推荐
How to solve the problem of missing language bar in win10 system
类模板中可变参的逐步展开
基于 xml 配置文件的入门级 SSM 框架整合
阅读别人的代码,是一种怎样的体验
[an Xun cup 2019]attack
[WUSTCTF2020]girlfriend
Redis 主从复制、哨兵模式、Cluster集群
Shell concise tutorial
每日3题(2):检查二进制字符串字段
my.ini文件配置
海量数据!秒级分析!Flink+Doris构建实时数仓方案
[business security-02] business data security test and example of commodity order quantity tampering
【微服务|Sentinel】热点规则|授权规则|集群流控|机器列表
Type 'image' is not a subtype of type 'imageprovider < object > solution
Resolve activity startup - lifecycle Perspective
Dynamic Networks and Conditional Computation论文简读和代码合集
AXI总线
Design and implementation of food recipe and ingredients website based on vue+node+mysql
[advanced mathematics] from normal vector to surface integral of the second kind
Why must Oracle cloud customers self test after the release of Oracle cloud quarterly update?