Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
A simple implementation using Arrays instead of hashmap. It uses a fixed size a...

Syed Hassan

Jan 14, 2022

A simple implementation using Arrays instead of hashmap. It uses a fixed size array of 255 limit because the characters in unicode or ascii are 255.

public int longestSubString(String s) {

int longest =0; int current=0; int[] charCount = new int[255]; int endPtr = 0; int strPtr = 0;

while(endPtr 1) {

if(current > longest) { longest = current; }

current = 0; charCount = new int[255]; strPtr++; endPtr = strPtr; // slide the window continue; }

current++;

if(current > longest) { longest = current; }

endPtr++;

}

return longest;

}

0

0

Comments
Comments
G
Garima 3 years ago

How about this solution: const longestSubStringWithDistinctChars = (str) => { let windowStart = 0; let maxLength = -Infinity; let charMap = {};

for (let windowEnd = 0; windowEnd < str.length; windowEnd++) { let rightChar = str[windowEnd]; if (!(rightChar in charMap)) {...

G
Garima 3 years ago

NVm I found the answer in the thread.

On this page