LeetCode Longest Substring Without Repeating Characters Problem Solution in Java
class Solution {
public int lengthOfLongestSubstring(String s) {
int slen = s.length();
int retVal = 0;
for(int ndx = 0; ndx < slen; ndx++) {
String sub = s.substring(ndx);
for(int offset = 0; offset < sub.length(); offset++) {
int dupPos = sub.indexOf(sub.charAt(offset), offset + 1);
if (dupPos >= 0) {
sub = sub.substring(0,dupPos);
}
}
retVal = Math.max(retVal, sub.length());
}
return retVal;
}
}
LeetCode Longest Substring Without Repeating Characters Problem Solution in Python
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
mx_len, start = 0,0
used = {}
for i, char in enumerate(s):
if char in used and start <= used[char]:
start = used[char] + 1
else:
mx_len = max(mx_len, i-start+1)
used[char] = i
return mx_len
0 Comments