当前位置:网站首页>LeetCode 3. Longest substring without repeated characters & sliding window
LeetCode 3. Longest substring without repeated characters & sliding window
2022-06-25 18:26:00 【7rulyL1ar】
Subject requirements
Link to the original title :3. Longest substring without repeating characters
The questions are as follows :
Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
Examples are as follows :
Example 1:
Input : s = “abcabcbb”
Output : 3
explain : Because the longest substring without repeating characters is “abc”, So its length is 3.
———————————————————————————————————————————
Example 2:
Input : s = “bbbbb”
Output : 1
explain : Because the longest substring without repeating characters is “b”, So its length is 1.
———————————————————————————————————————————
Example 3:
Input : s = “pwwkew”
Output : 3
explain : Because the longest substring without repeating characters is “wke”, So its length is 3.
Please note that , Your answer must be Substring The length of ,“pwke” Is a subsequence , Not substring .
———————————————————————————————————————————
Example 4:
Input : s = “”
Output : 0
solution : The sliding window
Ideas
From the beginning, the string s Traversal , The character traversed each time is used as the starting character of the substring , Then find the longest non repeated substring starting with the current character , One traverse can solve the problem .
You can use one Set Set to maintain the longest non repeated substring stored in each traversal to optimize the ordinary traversal . Suppose the existing string abcda, Then the longest non repeated longest string traversed for the first time is abcd, The corresponding second traversal role bcda, among bcd Part is that it is unnecessary to repeat the comparison in the second traversal , So you can use a collection to store , Remove each iteration Set Set first , The rest of the string must be non repeating , Continue to compare the set after the original string , Space for time .
In the process of problem solving, a pointer always points to the starting position of the new child string , The other pointer always looks for and points to the end of the current longest non repeating substring , Two pointers dynamically form an indefinite length substring , Hence the name sliding window , The essence is a kind of double pointer .
complete AC Code
class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<Character>();
int max = 0;
int right = -1;
for(int i = 0; i < s.length(); i++){
if(i != 0)set.remove(s.charAt(i - 1));
while(right + 1 < s.length() && !set.contains(s.charAt(right + 1)))set.add(s.charAt(right++ + 1));
max = Math.max(max, set.size());
}
return max;
}
}
Complexity analysis
Time complexity :O(N),n String length , In fact, both pointers need to traverse the string once. The time complexity O(2N), Therefore, the total time complexity O(N).
Spatial complexity :O(S),S Indicates the total number of characters that may be contained in the string .
边栏推荐
- [deeply understand tcapulusdb technology] create a game area for document acceptance
- [deeply understand tcapulusdb technology] transaction execution of document acceptance
- 【深入理解TcaplusDB技术】单据受理之表管理
- RMAN backup database_ Duplexing backup sets
- Lazy singleton mode from shallow to deep
- LeetCode力扣(剑指offer 26-30)26. 树的子结构 27. 二叉树的镜像 28. 对称的二叉树 29. 顺时针打印矩阵 30. 包含min函数的栈
- connect to address IP: No route to host
- IVX 启航
- How to open a stock account is it safe to open an account
- Redis6
猜你喜欢
![[compréhension approfondie de la technologie tcaplusdb] sauvegarde des données d'affaires tcaplusdb](/img/7f/6d42dc96348001dd6ebd724a44a123.png)
[compréhension approfondie de la technologie tcaplusdb] sauvegarde des données d'affaires tcaplusdb

IVX 启航
![[in depth understanding of tcapulusdb technology] new models of tcapulusdb](/img/10/f94a5e1ebeaa803c754dd77351950f.png)
[in depth understanding of tcapulusdb technology] new models of tcapulusdb

Centos7 installing redis 7.0.2

1. Understanding of norm

Article 6:clion:toolchains are not configured configure disable profile

Optimization of lazyagg query rewriting in parsing data warehouse

【深入理解TcaplusDB技术】单据受理之事务执行

【深入理解TcaplusDB技术】单据受理之表管理

【深入理解TcaplusDB技术】TcaplusDB新增机型
随机推荐
Wechat applet reports an error: request:fail URL not in domain list
Matlab中图形对象属性gca使用
Slam visuel Leçon 14 leçon 9 filtre Kalman
158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能
Redis trend - NVM memory
How to delay the delay function
SQL Server real time backup library requirements
RMAN备份数据库_使用RMAN做拆分镜像(split mirror)备份
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
华为云SRE确定性运维专刊(第一期)
【深入理解TcaplusDB技术】TcaplusDB构造数据
中断操作:AbortController学习笔记
el-table高度自适应
Android Internet of things application development (smart Park) - picture preview interface
【深入理解TcaplusDB技术】TcaplusDB新增机型
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
The stacks 2022:32 marketing technology stacks selected
RMAN backup database_ Restart RMAN backup
Dell r530 built in hot spare status change description
JVM understanding