Created
September 4, 2017 23:13
-
-
Save codediodeio/24319b9b17cba57e7a34002228abaaaf to your computer and use it in GitHub Desktop.
Longest Substring JavaScript - LeetCode Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given a string, find the length of the longest substring without repeating characters. | |
// Examples: | |
// Given "abcabcbb", the answer is "abc", which the length is 3. | |
// Given "bbbbb", the answer is "b", with the length of 1. | |
// Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. | |
var lengthOfLongestSubstring = function(s) { | |
let map = {} | |
let start = 0 | |
let maxLen = 0 | |
let arr = s.split('') | |
for (i=0; i < s.length; i++) { | |
let current = map[arr[i]] | |
if (current!=null && start <= current) { | |
start = current + 1 | |
} else { | |
maxLen = Math.max(maxLen, i - start + 1) | |
} | |
map[arr[i]] = i | |
} | |
return maxLen | |
}; |
How to display those final string. In this it return only length
https://github.com/khaled301/js-excercise-code/blob/leet-code/long-substring/long-substring.js
var str = "abcasdpocaaws";
var array = {};
maxCount = 0;
var counter = 0;
for (var i = 0; i < str.length; i++) {
if (array[str[i]]) {
if (counter > maxCount) {
maxCount = counter
}
counter = 0;
array = [];
}
counter++;
array[str[i]] = true;
}
console.log(maxCount)
function getLongestSubString(str) {
if(!str.trim()) {
return "Please enter string";
}
const strArray = str.trim().split('');
let count = 0, maxLength = 0, index;
for(let i = 0; i<strArray.length; i++) {
if(strArray[i] === strArray[i+1]) {
count++;
} else {
if(count> maxLength) {
maxLength = count;
index=i;
}
count=0;
}
}
const subStrIndex = index - maxLength;
const subString = str.slice(subStrIndex, subStrIndex + maxLength+1)
if(subString) {
return {index: subStrIndex, length: maxLength + 1, subString: subString };
}
return "No sub-string found";
}
const str = "aabbbcd";
console.log(largestSubString(str)) // {index: 2, length: 3, subString: "bbb", }
function maxSubstring(s) {
// Write your code here
const array = []
const lengthS = s.length
const pusher = (value) => {
if (value !== '') {
if (array.length > 0) {
if (array.indexOf(value) === -1) {
array.push(value)
}
} else {
array.push(value)
}
}
}
pusher(s)
for (const [index, value] of s.split('').entries()) {
let length = lengthS
let string = s
const indexO = s.indexOf(value)
pusher(value)
while (length > indexO) {
pusher(string.slice(index-1, length + 1))
length = --length
}
string = s.slice(index, lengthS)
}
array.sort()
return array.pop()
}
let str = "abcabcbbb";
let seen = {};
let start = 0;
let maxLength = 0;
[...str].map((itm, i) => {
if(itm in seen && start <= seen[itm]) {
start = seen[itm];
maxLength = Math.max(i - start, maxLength)
}
seen[itm] = i
})
console.log(maxLength)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to display those final string. In this it return only length