Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 36dcff8

Browse files
Abhijit-033appgurueu
andauthored
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)

AltStyle によって変換されたページ (->オリジナル) /