Last active
October 9, 2022 04:46
-
-
Save manavm1990/052c2b51678d9e64300ccb09636381e7 to your computer and use it in GitHub Desktop.
Some expert level JS challenges (according to Hackernoon post)
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
export const getLenOfLongestSubstringWithoutRepeatingChars = ( | |
str: string, | |
): number => { | |
// If the string is empty, return 0 | |
if (str.length === 0) { | |
return 0; | |
} | |
// Start building a substring from the first character until we duplicate a character | |
return str | |
.split('') | |
.reduce( | |
(acc, char, index, origin) => { | |
const lastI = acc.length - 1; | |
// Either append to the last string in 'acc' or start a new one (if duplicate char) | |
const lastString = acc[lastI]; | |
if (lastString.includes(char)) { | |
// Append a new string that goes back to the last occurrence of the duplicate char (+ 1) | |
return [...acc, str.slice(str.lastIndexOf(char, index - 1), index)]; | |
} | |
acc[lastI] = lastString + char; | |
return acc; | |
}, | |
[''], | |
) | |
.sort((a, b) => b.length - a.length)[0].length; | |
}; | |
export const restructureArrayNumsFirstLettersSecond = ( | |
numsAndLetters: (number | string)[], | |
sorted = false, | |
) => { | |
const nums = numsAndLetters.filter( | |
(item) => typeof item === 'number', | |
) as number[]; | |
const letters = numsAndLetters.filter( | |
(item) => typeof item === 'string', | |
) as string[]; | |
if (sorted) { | |
return [...nums.sort((a, b) => a - b), ...letters.sort()]; | |
} | |
return [...nums, ...letters]; | |
}; |
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
import { it, describe, expect, test } from 'vitest'; | |
import { | |
getLenOfLongestSubstringWithoutRepeatingChars, | |
restructureArrayNumsFirstLettersSecond, | |
} from '.'; | |
describe('getLenOfLongestSubstringWithoutRepeatingChars', () => { | |
test('Find the length of the longest substring without repeating characters', () => { | |
const input = 'abbbcabcdefef'; | |
const output = 6; | |
expect(getLenOfLongestSubstringWithoutRepeatingChars(input)).toBe(output); | |
}); | |
it('should return 0 for empty string', () => { | |
const input = ''; | |
const output = 0; | |
expect(getLenOfLongestSubstringWithoutRepeatingChars(input)).toBe(output); | |
}); | |
}); | |
describe('restructureArrayNumsFirstLettersSecond (sorted 2)', () => { | |
const input = [2, 'b', 4, 'd', 3, 'a', 'c', 'e', 5, 1]; | |
it('should restructure the array', () => { | |
const output = [2, 4, 3, 5, 1, 'b', 'd', 'a', 'c', 'e']; | |
expect(restructureArrayNumsFirstLettersSecond(input)).toEqual(output); | |
}); | |
it('should restructure and sort the array', () => { | |
const output = [1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e']; | |
expect(restructureArrayNumsFirstLettersSecond(input, true)).toEqual(output); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment