[英]Length of the Longest Common Substring without repeating characters
Given "abcabcbb", the answer is "abc", which the length is 3. 给定“ abcabcbb”,答案为“ abc”,长度为3。
Given "bbbbb", the answer is "b", with the length of 1. 给定“ bbbbb”,答案为“ b”,长度为1。
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. 给定“ pwwkew”,答案为“ wke”,长度为3。请注意,答案必须是一个子字符串,“ pwke”是一个子序列,而不是子字符串。
I have came up with a solution that worked, but failed for several test cases. 我想出了一个可行的解决方案,但在多个测试案例中失败了。 I then found a better solution and I rewrote it to try and understand it.
然后,我找到了一个更好的解决方案,并将其改写为尝试并理解它。 The solution below works flawlessly, but after about 2 hours of battling with this thing, I still can not understand why this particular line of code works.
下面的解决方案可以完美地工作,但是经过大约2个小时的奋斗,我仍然不明白为什么这行代码行得通。
import java.util.*;
import java.math.*;
public class Solution {
public int lengthOfLongestSubstring(String str) {
if(str.length() == 0)
return 0;
HashMap<Character,Integer> map = new HashMap<>();
int startingIndexOfLongestSubstring = 0;
int max = 0;
for(int i = 0; i < str.length(); i++){
char currentChar = str.charAt(i);
if(map.containsKey(currentChar))
startingIndexOfLongestSubstring = Math.max(startingIndexOfLongestSubstring, map.get(currentChar) + 1);
map.put(currentChar, i);
max = Math.max(max, i - startingIndexOfLongestSubstring + 1);
}//End of loop
return max;
}
}
The line in question is 有问题的行是
max = Math.max(max, i - startingIndexOfLongestSubstring + 1);
I don't understand why this works. 我不明白为什么会这样。 We're taking the max between our previous max, and the difference between our current index and the starting index of what is currently the longest substring and then adding 1. I know that the code is getting the difference between our current index, and the startingIndexOfSubstring, but I can't conceptualize WHY it works to give us the intended result;
我们将先前的最大值与当前索引与当前最长的子字符串的起始索引之间的差值相加,然后加1。我知道代码正在获取当前索引与当前索引之间的差值startingIndexOfSubstring,但是我无法概念化为什么它可以给我们预期的结果; Can someone please explain this step to me, particularly WHY it works?
有人可以向我解释此步骤,尤其是为什么它有效吗?
I'm usually bad at explaining, let me give it a shot by considering an example. 我通常不好解释,让我考虑一个例子。
String is "wcabcdeghi". 字符串是“ wcabcdeghi”。
Forget the code for a minute and assume we're trying to come up with a logic. 暂时忘记代码,并假设我们正在尝试提出一个逻辑。
map.put(currentChar, i);
) map.put(currentChar, i);
) max
max
startingIndexOfLongestSubstring
to keep track of this. startingIndexOfLongestSubstring
来跟踪此情况。 (This should've been named startingIndexOfNonRepetativeCharacter, then again I'm bad with naming as well). startingIndexOfNonRepetativeCharacter
) so to know the length of current sub-string all I need to do is ( In code - ) i - startingIndexOfLongestSubstring + 1
(current character position - The non-repetative character length + (subtraction doesn't do inclusive of both sides so adding 1). Lets call this currentLength
startingIndexOfNonRepetativeCharacter
),以便知道我需要做的当前子字符串的长度是( 在代码中 ) i - startingIndexOfLongestSubstring + 1
(当前字符位置-非重复字符长度+(减法不包括正反两面,因此加1)。让我们将此称为currentLength
currentLength
can break our max. currentLength
可以破坏最大值。 So ( In code - ) max = Math.max(max, i - startingIndexOfLongestSubstring + 1);
max = Math.max(max, i - startingIndexOfLongestSubstring + 1);
startingIndexOfLongestSubstring = map.get(currentChar)
. startingIndexOfLongestSubstring = map.get(currentChar)
。 So why are we doing a Max
? Max
? startingIndexOfLongestSubstring
). startingIndexOfLongestSubstring
之前,才startingIndexOfLongestSubstring
地图中选择一个字符)。 Hope I've answered all lines in the code and mainly If the explanation was understandable. 希望我已经回答了代码中的所有行,主要是如果解释是可以理解的。
Because 因为
i - startingIndexOfLongestSubstring + 1
is amount of characters between i
and startingIndexOfLongestSubstring
indexes. 是
i
和startingIndexOfLongestSubstring
索引之间的字符数。 For example how many characters between position 2 and 3? 例如,位置2和3之间有多少个字符?
3-2=1
but we have 2 characters: on position 2 and position 3. 3-2=1
但我们有2个字符:在位置2和位置3上。
I've described every action in the code: 我已经在代码中描述了每个动作:
public class Solution {
public int lengthOfLongestSubstring(String str) {
if(str.length() == 0)
return 0;
HashMap<Character,Integer> map = new HashMap<>();
int startingIndexOfLongestSubstring = 0;
int max = 0;
// loop over all characters in the string
for(int i = 0; i < str.length(); i++){
// get character at position i
char currentChar = str.charAt(i);
// if we already met this character
if(map.containsKey(currentChar))
// then get maximum of previous 'startingIndexOfLongestSubstring' and
// map.get(currentChar) + 1 (it is last occurrence of the current character in our word before plus 1)
// "plus 1" - it is because we should start count from the next character because our current character
// is the same
startingIndexOfLongestSubstring = Math.max(startingIndexOfLongestSubstring, map.get(currentChar) + 1);
// save position of the current character in the map. If map already has some value for current character
// then it will override (we don't want to know previous positions of the character)
map.put(currentChar, i);
// get maximum between 'max' (candidate for return value) and such value for current character
max = Math.max(max, i - startingIndexOfLongestSubstring + 1);
}//End of loop
return max;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.