[LeetCode] Longest Substring Without Repeating Characters

Well, there many ways to solve this problem. Let's first look at a naive solution.

The basic idea is simple. Starting from the first character of the string, we visit the next character sequentially. If no duplicates appear, we count the length and update the maximum length. When we meet a repeated character, we find the position of its previous appearance (denoted as pre) and set the starting position to be pre + 1 and repeat the above process till the ending point has exceeded the length of the string. To implement this algorith, we need a hash table to record the last occurrence of each character we see and two pointers for the starting and ending position.

The code is as follows.

复制代码
 1     // Naive implementation using two pointers
 2     int lengthOfLongestSubstring(string s) {
 3         int pos[256];
 4         memset(pos, -1, sizeof(pos));
 5         int start = 0, end = 0, maxlen = 0;
 6         while (end < (int)s.length()) {
 7             if (pos[s[end]] >= 0) {
 8                 maxlen = max(maxlen, end - start);
 9                 start = pos[s[end]] + 1;
10                 end = start;
11                 memset(pos, -1, sizeof(pos));
12             }
13             else pos[s[end]] = end++;
14         }
15         return max(maxlen, end - start);
16     }
复制代码

I hope this code to be self-explanatory enough. In fact, we can see that the record of end is to obtain the length of the current substring. So we may simply use another variable to store the length directly. The code then becomes as follows.

复制代码
 1     // Naive implementation using one pointer
 2     int lengthOfLongestSubstring(string s) {
 3         int pos[256];
 4         memset(pos, -1, sizeof(pos));
 5         int start = 0, len = 0, maxlen = 0;
 6         while (start < (int)s.length()) {
 7             if (pos[s[start]] >= 0) {
 8                 maxlen = max(maxlen, len);
 9                 start = pos[s[start]] + 1;
10                 memset(pos, -1, sizeof(pos));
11                 len = 0;
12             }
13             else {
14                 pos[s[start]] = start++;
15                 len++;
16             }
17         }
18         return max(maxlen, len);
19     }
复制代码

Both of the above codes have called memset several times and thus take much time. In fact, only the characters after the new starting point (inclusive) need to be reset. So we can further reduce the amout of work. We can use a boolean arary to denote whether the element has been appeared and reset those after the new starting point (inclusive).

The following code runs much faster!

复制代码
 1     int lengthOfLongestSubstring(string s) {
 2         bool exist[256] = {false};
 3         int start = 0, end = 0, maxlen = 0;
 4         while (end < (int)s.length()) {
 5             if (exist[s[end]]) {
 6                 maxlen = max(maxlen, end - start);
 7                 while (s[start] != s[end])
 8                     exist[s[start++]] = false;
 9                 start++;
10                 end++;
11             }
12             else exist[s[end++]] = true;
13         }
14         return max(maxlen, end - start);
15     }
复制代码

One of my friend suggests the following code. It may be hard to understand for the first time. The trick is that it records the length of the current substring and updates it whenever a duplicate occurred.

复制代码
 1     int lengthOfLongestSubstring(string s) {
 2         int pos[256];
 3         memset(pos, -1, sizeof(pos));
 4         int len = 0, maxlen = 0;
 5         for (int i = 0; i < s.length(); i++) {
 6             int pre = pos[s[i]];
 7             if (pre == -1 || i - len > pre)
 8                 len++;
 9             else {
10                 maxlen = max(maxlen, len);
11                 len = i - pre;
12             }
13             pos[s[i]] = i;
14         }
15         return max(maxlen, len);
16     }
复制代码

Some nice people posted the following short DP code in the LeetCode discuss. The code is damn cool! Please refer to this link for more explanations.

复制代码
 1     // Dynamic Programming
 2     int lengthOfLongestSubstring(string s) {
 3         int pos[256];
 4         memset(pos, -1, sizeof(pos));
 5         int start = 0, maxlen = 0;
 6         for (int i = 0; i < s.length(); i++) {
 7             start = max(start, pos[s[i]] + 1);
 8             maxlen = max(maxlen, i - start + 1);
 9             pos[s[i]] = i;
10         }
11         return max(maxlen, (int)s.length() - start);
12     }
复制代码
posted @   jianchao-li  阅读(207)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· 从零开始开发一个 MCP Server!
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
点击右上角即可分享
微信分享提示