[LeetCode] Longest Substring Without Repeating Characters

本文探讨了多种求解最长无重复字符子串长度的方法,包括朴素实现、使用布尔数组减少重置工作量及动态规划等高效算法。通过不同代码示例展示了如何优化算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值