[Java] Leetcode 3. Longest Substring Without Repeating Characters [Sliding Windows #3]

preview_player
Показать описание
In this video, I'm going to show you how to solve Leetcode 3. Longest Substring Without Repeating Characters which is related to Sliding Windows.

Here’s a quick rundown of what you’re about to learn:

Course Contents
(0:00​) Question Walkthrough
(2:05) Solution Explain
(6:13) Code

In the end, you’ll have a really good understanding on how to solve Leetcode 3. Longest Substring Without Repeating Characters and questions that are similar to this Sliding Windows.

Now, if you want to get good at Sliding Windows, please checkout my Sliding Windows playlist.

Рекомендации по теме
Комментарии
Автор

Nice explanation ... generally i code in cpp bt see your explanation

FunForestFood
Автор

the nested while loop is not needed, can be replace with 'if'

kseniaeugene
Автор

Hi Eric, great videos, thanks for sharing!

Just a quick question(7:30): Why every time we need to convert a string to charArr[], then get the lengh of the array, rather than directly getting the length of the string (e.g. int n = s.length)?

pengx
Автор

class Solution {
public int s) {


char arr[] = s.toCharArray();
int n = arr.length;
if (n<2) return n;

// define pointers //

int L =0, R = 0;

// DEFINE TABLE //

Map<Character, Integer> hm = new HashMap<>();

// define max len //
int maxLen = 0;

// find longest substring //
while(R<n) {
// add current element
hm.put(arr[R], hm.getOrDefault(arr[R], 0) + 1);

//check if we meet the condition //
while(hm.getsize()!= R-L+1) {
hm.put(arr[L], hm.get(arr[L]) - 1);
if(hm.get(arr[L] == 0)) {
hm.remove(arr[L]);
}
L++;

}
// update the maxlen //
maxLen = Math.max(maxLen, R- L +1);

// move the R one to the right //

R++;

}
// return maxlen //

return maxLen;
}
}
code is not working . getting error ..

mrb
join shbcf.ru