L3. Longest Substring Without Repeating Characters | 2 Pointers and Sliding Window Playlist

take U forward
26 Mar 202423:09

Summary

TLDRIn this educational video, the presenter dives into the problem of finding the longest substring without repeating characters using the sliding window and two-pointer technique. They start with a brute force approach, explaining its limitations and time complexity, then transition to a more efficient solution involving a hash array to track character indices. The presenter guides viewers through the optimized algorithm step by step, highlighting its linear time complexity and constant space complexity due to the fixed-size hash array, wrapping up with a pseudo code for clarity.

Takeaways

  • 📘 The video is a continuation of an A to Z DSA course focusing on a problem involving the concept of sliding window and two-pointer techniques.
  • 🔍 The problem presented is to find the longest substring without repeating characters in a given string that can contain any character from the 256 possible characters.
  • 📌 The definition of a substring is explained as any consecutive portion of the string, and the task is to find the longest such substring without repeating characters.
  • 🤔 A brute force approach is initially discussed, which involves generating all possible substrings and checking for repeating characters, but this method is inefficient.
  • 🚫 The script emphasizes that if a repeating character is found, there's no need to continue checking further substrings as they will also contain repetitions.
  • 🔑 The use of a hash array of size 256 is introduced to keep track of characters that have been seen and their last seen index to optimize the search for the longest substring.
  • 🔄 The sliding window technique is introduced as an optimized approach, which involves maintaining a window of non-repeating characters and adjusting it based on the presence of repeating characters.
  • 📈 The time complexity of the brute force approach is analyzed to be O(n^2), which is not efficient, and the need for optimization to around O(n) is discussed.
  • 📝 Pseudo code is provided to demonstrate the sliding window algorithm, which includes initializing a hash array, maintaining left and right pointers, and updating the maximum length found.
  • 🕊️ The optimized sliding window approach has a time complexity of O(n) and a space complexity of O(1), making it a more efficient solution to the problem.
  • 👍 The video concludes with an invitation for viewers to like and subscribe if they found the content helpful and understood the material.

Q & A

  • What is the main problem discussed in the video script?

    -The main problem discussed in the video script is finding the longest substring without repeating characters in a given string.

  • What is a substring as defined in the script?

    -A substring is any portion of the string that is consecutive in nature. For example, 'ca' is a substring of 'cadebz', but 'c' and 'C' are not consecutive and thus not a substring.

  • What is the brute force approach to solving the problem mentioned in the script?

    -The brute force approach involves generating all possible substrings starting from each character and checking for repeating characters. If a repeat is found, the process stops for that particular substring.

  • How does the script suggest optimizing the brute force solution?

    -The script suggests using a two-pointer and sliding window approach to optimize the solution, which is more efficient than the brute force method.

  • What is the role of the hash array in the optimized solution?

    -The hash array is used to remember the last seen index of each character. It helps in efficiently checking if a character has already appeared in the current window of the substring being considered.

  • What is the time complexity of the brute force approach mentioned in the script?

    -The time complexity of the brute force approach is O(n^2), where n is the length of the string, due to the nested loops required to generate all substrings.

  • What is the space complexity of the brute force approach?

    -The space complexity of the brute force approach is O(1) since no additional space is used that scales with the input size, aside from the temporary variables.

  • How does the two-pointer and sliding window approach improve the time complexity?

    -The two-pointer and sliding window approach improves the time complexity to O(n), as it only requires a single pass through the string with constant time operations for each character.

  • What is the space complexity of the optimized solution using the hash array?

    -The space complexity of the optimized solution is O(256), which corresponds to the size of the hash array needed to store the indices of the last seen characters for all possible ASCII values.

  • How does the script describe the process of updating the left and right pointers in the sliding window approach?

    -The script describes updating the right pointer to expand the window and checking the hash array for repeating characters. If a repeat is found, the left pointer is moved to the right of the last occurrence of the repeating character to maintain the no-repeat condition, and the window is then expanded again.

Outlines

00:00

📘 Introduction to the Sliding Window Concept

The video begins with an introduction to a Data Structures and Algorithms (DSA) course, focusing on the sliding window and two-pointer technique. The presenter welcomes viewers and introduces the problem of finding the longest substring without repeating characters within a given string. The string can contain any character, and the task is to determine the maximum length of a substring where all characters are unique. The presenter explains the concept of a substring and provides examples to clarify the problem statement. The naive solution of generating all possible substrings is discussed, along with the idea of using two pointers, 'i' and 'j', to iterate through the string and generate substrings efficiently.

05:00

🔍 Analyzing the Brute Force Approach

The presenter delves into the brute force method of generating all possible substrings to find the longest non-repeating character substring. This approach involves checking each character against a hash table to ensure no repetitions. The time complexity of this method is analyzed, revealing it to be O(n^2), which is inefficient. The need for optimization is highlighted, and the presenter suggests using a more advanced technique involving a sliding window and two pointers to improve efficiency.

10:02

🛠️ Implementing the Sliding Window Technique

The video script outlines the implementation of the sliding window technique to solve the problem more efficiently. The presenter explains the use of a hash array to track the last seen index of each character and two pointers, 'left' and 'right', to represent the current window. As the 'right' pointer expands the window, the script details how to check for repeating characters and when to move the 'left' pointer to maintain a valid window without repeating characters. The process of updating the maximum length of the substring and the hash array as the 'right' pointer moves through the string is described in detail.

15:04

🔄 Sliding Window Algorithm Walkthrough

This paragraph provides a step-by-step walkthrough of the sliding window algorithm. The presenter discusses the initial setup with an empty window and the first character. As the 'right' pointer advances, the algorithm checks for repeating characters by consulting the hash array. If a repeat is found, the 'left' pointer is moved to the right of the last occurrence of the repeating character. The length of the current window is compared with the maximum length found so far, and the maximum is updated if necessary. The process continues until the 'right' pointer reaches the end of the string, at which point the algorithm concludes.

20:05

📝 Pseudo Code and Complexity Analysis

The presenter provides pseudo code for the sliding window algorithm, emphasizing its language-agnostic nature. The pseudo code includes initialization of the hash array, pointers, and maximum length variable. It details the loop that moves the 'right' pointer and checks for repeating characters, updating the 'left' pointer and maximum length as needed. After presenting the pseudo code, the time and space complexity of the algorithm are analyzed. The time complexity is identified as O(n) due to the single pass through the string with constant-time operations, and the space complexity is O(1) because the hash array size is constant and does not grow with input size.

Mindmap

Keywords

💡Sliding Window

The 'Sliding Window' is a technique used in computer science to solve problems that involve a subset of an array or a string. In the context of this video, it is used to find the longest substring without repeating characters. The script describes how to employ a sliding window with two pointers to keep track of the current substring and to efficiently find the maximum length of a substring without duplicates.

💡Two-pointer Technique

The 'Two-pointer Technique' is an algorithmic approach that uses two indices to traverse a sequence, such as an array or a string. In the video, this technique is combined with the sliding window concept to solve the problem of finding the longest non-repeating substring. The pointers are used to expand and contract the window, ensuring that the substring within the window does not contain any repeating characters.

💡Substring

A 'Substring' is any sequence of characters that is contained within a larger string and is contiguous. The script explains the concept of a substring and how it relates to the problem of finding the longest substring without repeating characters. The term is used to illustrate the portion of the string that is currently being considered in the algorithm.

💡Hashing

In computer science, 'Hashing' refers to the process of converting a value into a corresponding index in a data structure known as a hash table. In the script, hashing is used to keep track of characters that have been seen in the current window of the string. It helps in quickly determining whether a character has already appeared in the substring, which is crucial for avoiding repeats.

💡Character

A 'Character' in this context refers to any single element of a string, which can be a letter, digit, or special symbol. The video discusses how the problem can involve any character from the possible 256 characters on a standard keyboard, emphasizing the need for the algorithm to be versatile in handling different types of characters.

💡Brute Force

'Brute Force' is a problem-solving approach that involves trying every possible solution until the correct one is found. The script mentions a brute force method for generating all possible substrings to find the longest non-repeating one. However, it also points out the inefficiency of this method and the need for optimization.

💡Optimization

In the context of algorithms, 'Optimization' refers to improving the efficiency of a solution, often by reducing the time or space complexity. The video script discusses the need to optimize the brute force solution to find the longest substring without repeating characters, suggesting the use of a sliding window and hashing for a more efficient approach.

💡Time Complexity

'Time Complexity' is a measure of the amount of time an algorithm takes to run as a function of the size of the input. The script analyzes the time complexity of the brute force approach and the optimized sliding window approach, explaining how the latter reduces the time complexity from O(n^2) to O(n), where 'n' is the length of the string.

💡Space Complexity

'Space Complexity' is a measure of the amount of memory an algorithm uses in relation to the size of the input. The script discusses the space complexity of the hashing approach, noting that it uses a fixed-size array of 256 elements, regardless of the input size, resulting in a space complexity of O(1), which is constant.

💡Index

An 'Index' in the context of this video refers to the position of a character within a string. The script uses the concept of index to explain how the two-pointer technique operates, with one pointer (left) moving based on the indices of previously seen characters, and the other pointer (right) expanding the window to consider new characters for the substring.

Highlights

Introduction to a problem-solving session on the concept of sliding window and two-pointer technique in a DSA course.

The problem involves finding the longest substring without repeating characters in a given string.

Explanation of what constitutes a substring and the importance of consecutive characters.

The string can contain any character from the 256 possible characters.

A brute force approach to generate all substrings and check for repeating characters.

The use of hashing to remember previously seen characters in the substring.

A detailed walkthrough of the brute force method with pseudo code.

Analysis of the time and space complexity of the brute force approach.

The interviewer's request to optimize the solution to improve efficiency.

Introduction to the two-pointer and sliding window technique as an optimization strategy.

How to use a hash array to track the indices of characters in the substring.

The process of expanding and contracting the window to avoid repeating characters.

Updating the pointers and hash map while considering the current substring.

The importance of careful updating of the left pointer to maintain the sliding window.

Writing pseudo code for the optimized sliding window technique.

Analysis of the time complexity of the optimized solution, aiming for linear time.

Explanation of the space complexity of the optimized solution, using a fixed-size hash array.

Conclusion of the video with a call to action for likes and subscriptions.

Closing remarks with a poetic touch on handling heartbreaks.

Transcripts

play00:04

so we will be continuing with this ders

play00:06

A to Z DSA course and today we will be

play00:08

solving a problem which involves the

play00:10

concept of sliding window and two-p

play00:13

pointer but before doing that hey

play00:14

welcome back to the channel I hope you

play00:16

guys are doing extremely well so the

play00:18

problem that we will be solving today is

play00:20

longest substring without repeating

play00:23

characters so the problem States you

play00:26

will be given a string which might have

play00:29

any character

play00:30

lower case upper case special character

play00:33

any possible character out of the 256

play00:36

characters that do exist on the planet

play00:39

can have any of them now your task is to

play00:42

figure out the longest

play00:45

substring what is the definition of a

play00:47

substring any portion of the string

play00:50

which is consecutive in nature b z e b

play00:55

is consecutive so this is a substring

play00:58

something like ca and C is not a

play01:01

substring because it is not consecuted

play01:04

now your task is to figure out the

play01:06

longest substring without repeating

play01:10

characters so if I take something like C

play01:12

A

play01:13

D this is a substring with a length

play01:18

three and every character is appearing

play01:21

once C appears once a appears once D

play01:24

appears once let's take c a d z c a d

play01:30

Zed is a

play01:33

possible oh sorry c a d b z c a d b z

play01:38

that is a possible substring which has a

play01:41

length five and each of the characters

play01:44

is appearing once only okay length five

play01:48

can I take this one C A DB z a no I

play01:52

cannot take this one why because a is

play01:56

appearing twice a is appearing twice so

play02:00

this cannot be a substring I cannot have

play02:02

repeating characters so thereby this

play02:05

could be a possible substring maybe this

play02:09

could be a possible substring because z

play02:11

a b c d doesn't have any repeating

play02:14

characters you can try out and you'll

play02:16

find out that the length five is the

play02:19

maximum that you can get and this is

play02:21

what you'll have to return not the

play02:23

subring just the length of the

play02:25

substring so if this problem comes up in

play02:27

an interview what is the extreme

play02:30

solution that you can think

play02:31

of probably generate all the substrings

play02:34

which is C is a possible substring C A

play02:38

is a possible substring c a d so

play02:40

basically c a c a d and then C A D B and

play02:44

then c a d b z so basically what I'm

play02:47

doing is I'm keeping on adding a

play02:49

character to character where C is my

play02:53

first character always so what I do is I

play02:55

start with C I keep on adding keep on

play02:57

adding keep on adding and eventually

play02:59

I'll cover all the sub strings that

play03:01

starts with C I can do the same with a I

play03:05

can start with a then I can add a d then

play03:07

a b then uh Zed and I can keep on adding

play03:12

and that will generate all the

play03:14

characters that starts with a similarly

play03:17

I can do it d d DB DB Z and I can keep

play03:21

on doing this very simple I can have a i

play03:24

pointer initially at C I can have a j

play03:27

and then I can move j then I can move j

play03:29

then I can move j

play03:30

and J will keep on adding to every

play03:33

substring that starts with C similarly

play03:35

for the next time I will move here and J

play03:38

will be here and then J will be here

play03:39

then J will be here and then eventually

play03:41

till the end similarly for the next time

play03:43

I will be here J will be here J will be

play03:45

here J will be here J will be here and J

play03:47

will go here so I can generate all the

play03:49

subrings by using I and J so maybe I can

play03:53

write the pseudo code then we'll be

play03:55

thinking about how to figure out that

play03:57

there are no repeating characters so I

play03:59

know that I equal to Z that's my first

play04:02

substring with C okay I can go ahead

play04:05

till n which is the size of the

play04:08

string I know I can have a j I can have

play04:11

a j that starts with i because that is

play04:14

the first character that will be there

play04:16

in the substring and then I can go ahead

play04:18

till n and I can do a

play04:21

j++ very simple what is the next thing I

play04:24

know one thing if I have to generate all

play04:26

the substrings I can take a sub equal to

play04:29

m empty string and I can keep on adding

play04:31

sub equal to plus s of J will this

play04:36

generate all the sub strings yes because

play04:39

initially I'll add C then I'll add a to

play04:42

it then D to it and I'll keep on adding

play04:45

everything so for the first time when I

play04:47

is zero I will generate all the

play04:49

substrings when I is one I'll generate

play04:51

all the substrings that starts with

play04:52

eight this all Loop structure kind of

play04:56

generates all the substrings that starts

play04:58

with uh

play05:00

C and then a and then D and then B then

play05:02

Zed and so on so I know the Brute Force

play05:05

to generate all the substrings what do I

play05:08

need to add I need to make sure there

play05:11

are no repeating characters let's

play05:13

analyze I start with C okay I start with

play05:16

C sorry I then add a then a d then a b

play05:23

then uh Zed and then uh a can I say

play05:29

something that

play05:30

the moment I get a a this is

play05:34

repeating shall I go beyond no because

play05:37

I'm looking for no repeating characters

play05:39

if I go beyond it c a d b z A and D we

play05:46

could possibly go beyond but then a is

play05:48

already repeating so there's no point in

play05:50

going Beyond so I can break out the

play05:52

moment I get a repeating character

play05:54

because beyond that it will always have

play05:56

a repeating character we've already seen

play05:58

it so I know something I'll have to take

play06:02

a concept like hashing so that I can

play06:05

keep in my memory that hey this a was

play06:08

seen previously this a was seen

play06:10

previously now we know one thing that

play06:12

there are 255 characters so what we

play06:15

could do is we could probably use the

play06:18

hashing technique where we can start

play06:20

from 0 till 255 why can I do that

play06:23

because let's assume you write int value

play06:28

equal to character a you know what

play06:30

happens a is asky value which is 97 goes

play06:34

into the value so smaller a is

play06:38

977 so each of these each of the

play06:41

characters that do exist on the planet

play06:43

starts from zero and ends set 255 when

play06:46

you convert them into integer the ask

play06:48

key format so I can probably do it so

play06:52

what I will do is I'll keep a hash array

play06:55

of 256 size initially with zero all of

play06:59

them are initialized with zero and then

play07:02

what I will do is whenever I'm getting a

play07:05

character which is basically the jth

play07:07

character basically the J character

play07:09

because I is standing here J is the one

play07:12

which is moving J is the one which is

play07:13

moving whenever I'm getting a character

play07:16

I will check was it seen previously was

play07:19

it seen previously I'm like okay let's

play07:22

maybe try to write down the

play07:24

go so be like hey if hash of s of J if

play07:31

you write this this possible thing gets

play07:33

converted into the integer format you

play07:35

don't have to do anything extraordinary

play07:37

and if this was seen previously you can

play07:40

break out because there's no point in

play07:41

going forward perfect and if this was

play07:44

not seen that means I'm okay I'm okay

play07:46

and if I'm okay can I compute the length

play07:50

imagine your your G is here and I is

play07:52

here what is the length this much is the

play07:54

length of the substring can I say the

play07:57

length is J minus I + 1 very yes and

play08:00

then I need to make sure I need the

play08:02

maximum maybe I can store the max length

play08:06

as zero and over here I can say max

play08:09

length equal to Max of length comma max

play08:15

length done and once I've done this I

play08:19

need to remember this I need to remember

play08:21

this J so maybe I can call the hash of G

play08:24

hash of s of G hey we have seen you so

play08:29

this a this a would this a will be

play08:31

marked I've seen you marked okay done

play08:35

finish Thea Loop finish Thea Loop and

play08:38

eventually you can go ahead and print

play08:39

out the max length so this will be

play08:42

exploring all the substrings and

play08:44

printing down the answer what will be

play08:46

the time complexity B go of

play08:49

n near about B of n so n into n is

play08:53

typically n squ what about the space

play08:56

complexity I'm using a hashing hash AR

play08:59

so that's we go of 256 because that is

play09:03

the size of the hash array I'm

play09:05

reinitializing it every that is

play09:07

perfectly okay this will be the time

play09:09

complexity and the space complexity and

play09:11

this is where the interviewer will not

play09:12

be happy with you and it'll ask you to

play09:14

optimize this so we need to optimize our

play09:16

previous solution how do I figure out

play09:19

what algorithm should I use first of all

play09:22

previous solution was taking n Square

play09:24

you've been asked to optimize it which

play09:26

means somewhere around n n login two and

play09:30

three somewhere around then right and

play09:33

over here there's something as substring

play09:36

so on any problem which involves finding

play09:39

the maximum substring or something like

play09:41

that you should straight away start

play09:43

thinking of a two pointer and a sliding

play09:46

window approach remember that okay so

play09:49

I'll be using a two pointer and sliding

play09:50

window it's not a specific algorithm

play09:53

it's rather a constructive algorithm

play09:55

which keeps on changing according to the

play09:57

problem statement the only that stays

play10:00

constant is the window you keep on

play10:02

moving the window how you move the

play10:04

window that depends on the problem

play10:05

statement and you'll have to do more and

play10:07

more problems you have to do a lot of

play10:09

dryon in order to understand two pointer

play10:11

and sliding window it's not a specific

play10:13

algorithm I repeat so first of all what

play10:16

I'll do is I'll

play10:18

take I'll note down the indexes that's

play10:21

the first thing I'll do so I've noted

play10:23

down all the

play10:24

indexes the next thing that I'll do is

play10:26

I'll take two pointers which is the left

play10:30

and the right okay and at the same time

play10:33

I'll take a hash why am I taking a hash

play10:36

I need to

play10:37

remember when did I see the character

play10:40

they'll understand as I move so I'll

play10:43

take a max length which is initially

play10:44

zero and in the hash I'll will be

play10:46

storing the character and the index

play10:48

where I've seen it previously so in

play10:51

sliding window whenever we consider L

play10:53

and R that means L2 R is the substring

play10:59

or the answer that I'm looking at so

play11:01

over here when I'm saying L is here R is

play11:03

here that means this is the substring

play11:06

that I'm considering got it first of all

play11:10

you're looking for no repeating

play11:11

characters so what I'll do is because

play11:14

this is my first character I'll go and

play11:17

look in the map have I seen C previously

play11:20

I haven't I haven't so what I'll do is

play11:23

I'll be like okay what is the length

play11:25

length is typically

play11:27

r- L + 1

play11:29

how much is R 0 how much is L 0 + one

play11:34

that's one and that's greater than my

play11:36

max length it's fine so what I'll do is

play11:39

I'll take C put the index and put it

play11:43

into my map or whatever data Str I'm

play11:45

using the next thing I'll do is I'll

play11:47

take R and I'll move it to the next

play11:49

which is at e this time I'm considering

play11:54

this to be my substring and for this to

play11:56

be considered my substring or my answer

play11:59

this a shouldn't have appeared

play12:01

previously shouldn't have appeared

play12:03

previously I look and I don't see it so

play12:06

I can take it so what will be my length

play12:09

1 - 0 + 1 which is 2 so I can update

play12:12

that to the max length because greater

play12:14

than maxent and at the same time I can

play12:17

take a and put it into my hashmap saying

play12:19

that a was seen at the first index

play12:22

perfect what is my next thing I will

play12:25

take R and I'll move it to

play12:28

D again I'm considering this to be my

play12:31

answer and for that D shouldn't be in

play12:33

the hashmap and it is not so thereby

play12:36

this can be a possible subring and the

play12:39

length will be three which is greater

play12:41

than the maximum length so I'll be

play12:43

updating the max length amazing and I'll

play12:45

take D and I'll put it into the hashmap

play12:48

at the second index next I'll move out

play12:51

to B again for this to be considered my

play12:54

answer B should have been in the hashm

play12:57

but it is not sorry shouldn't be in the

play12:59

Ash and it is not and if it is not can I

play13:03

consider the length to be four I can

play13:06

that's greater than max length update it

play13:08

the same time take B and put it into the

play13:11

hash let's take R and move it forward

play13:14

this time R is at Z so if R is at Z

play13:18

again for this to be considered my

play13:21

hashmap Z is being added correctly SOC

play13:25

said are you in the hash says no I'm not

play13:27

in the hash so but it is not in the ash

play13:30

map I can say that this is the length

play13:33

which is pi which is greater than max

play13:35

length so max length can be updated

play13:38

perfect what is the next job a z and put

play13:41

it into your hashmap nice let's move on

play13:45

now what is

play13:47

r r is at a so I'm saying this is my

play13:50

possible substring but but but when I

play13:53

look for a I see a at the first index I

play13:56

see a at the first index so can I see

play13:59

this can I see this that this left

play14:04

pointer will be here can I see no

play14:08

because if I keep it here then there

play14:10

will still be repeating characters so L

play14:14

typically goes to wherever I have seen a

play14:17

wherever let's say this is map wherever

play14:20

I've seen this a + one that's where

play14:24

that's where it will go to correct or

play14:25

not and what is this a can I say this a

play14:28

to be nothing

play14:30

but this one this character which is s

play14:33

of R correct so left will go to one

play14:37

place ahead of that so I'll say okay

play14:40

maybe I can keep it here then I'll not

play14:43

find that then I'll not find that like

play14:45

okay then you'll not find that a for

play14:47

sure and if this happens the length

play14:49

turns out to be four which is not

play14:52

greater than Max Lin so I do not update

play14:54

it I do not update it but before moving

play14:57

the r the last a has been seen here so

play15:01

I'll go ahead and update this to Five

play15:03

I'll update the a to five so I can go

play15:06

ahead and update the E to five and again

play15:08

move on

play15:11

perfect this time I'm saying that this

play15:15

is my window and this is my answer I'm

play15:17

like okay have I seen B I have seen b at

play15:21

the third index I've seen b at the third

play15:23

index okay have seen b at the third

play15:26

index I'm like I can definitely not

play15:29

consider this but what can I consider

play15:32

one more this one L can move here

play15:35

because cannot consider B okay make

play15:37

sense so I'll take L and I'll move it

play15:40

you so what is the length now the length

play15:43

turns out to be three which is

play15:45

definitely not more than Max Lin perfect

play15:48

let's move R but before that before that

play15:50

before that please go ahead and update

play15:53

this to six and then you'll move on

play15:55

update B to six now what is the next one

play15:59

next one

play16:01

is this one and I'm saying C is to be

play16:04

added and I'm like can I add C can I add

play16:09

C like yes yes this could be a possible

play16:12

sub this could be a possible subring

play16:14

because C is not in the substring but if

play16:17

you look properly if you look properly C

play16:20

is in the hashmap C is in the

play16:23

hashmap can I consider this I'm like on

play16:26

the naked eye yes but if you look look

play16:29

carefully see is there in the hashmap so

play16:31

you'll have to be careful you'll have to

play16:33

be careful when you're updating L please

play16:36

make sure please make sure the C that

play16:39

you're seeing that should be after L

play16:42

after not before because this is the

play16:44

portion you're considering this is the

play16:45

portion you're considering so whatever

play16:47

map you have seen whatever map what is

play16:50

basically map of s of R because s of R

play16:54

is C that should be greater than equal

play16:57

to l then only because this is the

play16:59

substring you're considering you

play17:01

shouldn't see beyond it before it no so

play17:04

C is not there C is not there there by

play17:07

the length is four and can I update no

play17:11

because I've got a better length perfect

play17:13

what will I do I'll update C to 7 I'll

play17:16

update C to 7 and I'll move

play17:19

out I'll move out to D this time when I

play17:22

look for D is there is there in the hash

play17:25

but but but but I'm only looking for

play17:27

this one and and L is at Fourth index

play17:30

whereas D is at second index so I do not

play17:33

update it and what is the length the

play17:35

length turns out to be five can I update

play17:39

no I already have five no need to update

play17:41

So eventually what happens

play17:44

is after this you update D to eight and

play17:48

then you move

play17:51

R and R goes out the moment it goes out

play17:55

you stop then and there got it so this

play17:58

is how the sliding window works you take

play18:00

initially an empty window and you start

play18:02

with the first and then you keep

play18:03

expanding keep expanding and the moment

play18:05

you get something which violates your

play18:07

condition you move the left you move the

play18:09

left got it so time to write down the

play18:11

pseudo code again this won't be any

play18:13

language specific code I'll be keeping

play18:15

it as generic as possible if you want

play18:18

your language specific code you can find

play18:19

them below so I'll be writing down the

play18:21

function now the function gets a string

play18:24

right string s what is the first thing

play18:27

that we do first of all we need to have

play18:30

a map data structure I know that there

play18:33

are 256 characters let's not Define a

play18:35

map data structure because that will be

play18:37

taking something like a logarithmic time

play18:39

so what I'll do is I'll take a hash

play18:42

array of 256 and initially I'll run a

play18:46

loop from 0 to 255 and make sure that it

play18:49

is initialized with minus one everywhere

play18:52

minus one means I haven't seen the video

play18:55

what is the next thing I need the left

play18:58

point po I need the right pointer both

play19:01

of them can be kept at the zero index

play19:04

and I need the max length and the max

play19:06

length is initially

play19:08

zero I I'll keep on moving the right

play19:10

till it doesn't exceeds so quite simple

play19:14

I can take n equal to S do size and I

play19:17

can keep while R is lesser than n very

play19:23

simple R is lesser than n what the first

play19:25

thing that I do I know this is my

play19:28

substring I check I check if this

play19:31

character is in the map or not okay

play19:34

let's do it if the character is in the

play19:39

map which is like hash off S of R is my

play19:42

character is it in the map if it is in

play19:45

the map that's not going to be minus one

play19:49

that means it is in the map that means

play19:52

it's in the

play19:54

map right so we know that it is not in

play19:58

the map what after that what if your L

play20:02

is standing here and R is standing here

play20:05

so you're looking for this particular

play20:07

substrate and the C is here which

play20:09

doesn't make sense because you're just

play20:11

looking for this particular substrate

play20:12

you'll have to check it's within L to R

play20:16

okay let's see where it is if hash of s

play20:22

of

play20:23

R if that's greater than equal to L that

play20:27

means it's within the range and I'll

play20:30

have to update l so the L will be

play20:34

updated to updated to S of R plus one so

play20:38

I've updated l once I've updated L I can

play20:42

finish so I know that if I've seen it l

play20:46

would have been updated if needed and

play20:49

then I can figure out the length which

play20:51

is length equal to r - L + 1 and I can

play20:56

say max length equal to Max of length

play21:01

comma max length very obvious and what

play21:05

we do next is before moving ahead please

play21:08

make sure you do an hash of s of R to

play21:12

the current index which is r at the same

play21:15

time move

play21:17

r++ this is what you will do and that's

play21:20

it yes that is it and eventually you can

play21:24

return the max

play21:27

length and that will be it so that was

play21:30

about the code now it's time to analyze

play21:32

the time complexity we have a wind Loop

play21:36

correct and the Y Loop is being run by

play21:39

the r variable and the r is moving by

play21:42

one one place where do we start we

play21:45

always start L and R with this one and

play21:48

at every step R is moving one step ahead

play21:50

and eventually it reaches the end R

play21:54

always keeps moving by one step L is

play21:57

moving but that is not not contributing

play22:00

to my while loop correct only the r is

play22:03

so the r moves from here to here which

play22:06

is the length so thereby the time

play22:10

complexity is B go of n not more than

play22:14

that because I'm just moving R the while

play22:16

loop is dependent on R and this one hash

play22:22

that's V of one because I've taken an

play22:24

array and to access the array it's B of

play22:26

one not B of login what about the space

play22:29

complexity the space complexity is beo

play22:31

of 256 which is pretty good so yes that

play22:36

will be it and that was TW pointer and

play22:38

sliding window algorithm not a specific

play22:40

algorithm you keep on taking it

play22:42

according to the problem statement so

play22:45

this will be it for this one and if

play22:46

you're still now watching and if you've

play22:48

understood everything please please do

play22:50

consider giving us a like and if you're

play22:51

new to our channel do consider

play22:53

subscribing to us as well with this I'll

play22:55

be wrapping up this video Let's SP some

play22:56

other the video till then byebye take

play22:57

care

play23:00

whenever your heart is

play23:02

broken don't ever forget your

play23:05

golden

play23:07

I

Rate This

5.0 / 5 (0 votes)

関連タグ
Sliding WindowSubstring ProblemAlgorithm OptimizationCharacter FrequencyHashing TechniqueData StructuresString ManipulationCoding InterviewEfficiency AnalysisDSA CourseCode Tutorial
英語で要約が必要ですか?