You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: longest_substring_without_repeating_char.js
+39-6Lines changed: 39 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -22,18 +22,51 @@ And then push this char at this index position to the resetted-newSubStr value.
22
22
C> And after all the above, at each iteration of the loop, we are reassigning the maxLen to be the max of maxLen till now and subArr.length. And by the end of all the iterations I am returning the final max.
newSubStr=newSubStr.slice(index+1);//Note, for slice() If endIndex is omitted, slice() extracts to the end of the string, when I want to extract / slice till the end of the string, I dont need to pass the second argument.
// SOLUTION-2 - The same exact logic, only difference is, I am not using a separate variable index here. In the above I am first checking if this item is duplication (if duplicate then fist slice) and only then I am pushing to the newSubStr the char. But here, I am combining both these steps in below.
53
+
54
+
lengthOfLongestSubstring_2=s=>{
55
+
56
+
letmax=0,newSubStr=[];
57
+
58
+
for(leti=0;i<s.length;i++){
59
+
60
+
// If 'i' is duplicated newSubStr.indexOf(i) will return the current index of that i and I will start slicing from the next position. And if its not duplicated, I will staring slicing from 0-index position and all the way upto the last char of newSubStr.
0 commit comments