Skip to content

Commit 36dcff8

Browse files
feat: New String Algorithm for LengthofLongestSubstringWithoutRepeati… (TheAlgorithms#1389)
* feat: New String Algorithm for LengthofLongestSubstringWithoutRepeatingCharacters * Fixed Errors and Mistakes in New Algorithm LengthofLongestSubstringWithoutRepetition * fix:TheAlgorithms#1389 Errors Fixed * fix:TheAlgorithms#1389 Syntax and Code Errors Fixed * fix:TheAlgorithms#1389 Errors FIxed * Fixed All new errors * fix:1389 Implemented suggestions and corrections * Use @see annotation --------- Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
1 parent b783612 commit 36dcff8

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @description : Given a string, the function finds the length of the longest substring without any repeating characters
3+
* @param {String} str - The input string
4+
* @returns {Number} The Length of the longest substring in a given string without repeating characters
5+
* @example lengthOfLongestSubstring("abcabcbb") => 3
6+
* @example lengthOfLongestSubstring("bbbbb") => 1
7+
* @see https://leetcode.com/problems/longest-substring-without-repeating-characters/
8+
*/
9+
10+
const lengthOfLongestSubstring = (s) => {
11+
if (typeof s !== 'string') {
12+
throw new TypeError('Invalid Input Type')
13+
}
14+
let maxLength = 0
15+
let start = 0
16+
const charMap = new Map()
17+
for (let end = 0; end < s.length; end++) {
18+
if (charMap.has(s[end])) {
19+
start = Math.max(start, charMap.get(s[end]) + 1)
20+
}
21+
charMap.set(s[end], end)
22+
maxLength = Math.max(maxLength, end - start + 1)
23+
}
24+
return maxLength
25+
}
26+
27+
export { lengthOfLongestSubstring }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { lengthOfLongestSubstring } from '../LengthofLongestSubstringWithoutRepetition'
2+
3+
describe('LengthOfLongestSubstring', () => {
4+
it('should throw error if parameter is not string', () => {
5+
expect(() => lengthOfLongestSubstring(345)).toThrowError(TypeError)
6+
expect(() => lengthOfLongestSubstring(true)).toThrowError(TypeError)
7+
expect(() => lengthOfLongestSubstring(null)).toThrowError(TypeError)
8+
})
9+
10+
it('should check substrings containing unique characters', () => {
11+
expect(lengthOfLongestSubstring('abcabcbb')).toBe(3)
12+
expect(lengthOfLongestSubstring('bbbbb')).toBe(1)
13+
expect(lengthOfLongestSubstring('pwwkew')).toBe(3)
14+
expect(lengthOfLongestSubstring(' ')).toBe(1)
15+
expect(lengthOfLongestSubstring('abcdefghijklmnaaaaa')).toBe(13)
16+
})
17+
18+
it('should give zero for empty strings', () => {
19+
expect(lengthOfLongestSubstring('')).toBe(0)
20+
})
21+
22+
it('should be case-sensitive', () => {
23+
expect(lengthOfLongestSubstring('AaBbCc')).toBe(3)
24+
expect(lengthOfLongestSubstring('AbCdEf')).toBe(6)
25+
})
26+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy