From f5d458538685bd868e67025e41fac232c4542ce8 Mon Sep 17 00:00:00 2001 From: Abhijit-033 Date: Sun, 1 Oct 2023 18:27:12 +0530 Subject: [PATCH 1/8] feat: New String Algorithm for LengthofLongestSubstringWithoutRepeatingCharacters --- ...ngthofLongestSubstringWithoutRepetition.js | 31 ++++++++++++++++++ ...fLongestSubstringWithoutRepetition.test.js | 32 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 String/LengthofLongestSubstringWithoutRepetition.js create mode 100644 String/test/LengthofLongestSubstringWithoutRepetition.test.js diff --git a/String/LengthofLongestSubstringWithoutRepetition.js b/String/LengthofLongestSubstringWithoutRepetition.js new file mode 100644 index 0000000000..5136fd3d3d --- /dev/null +++ b/String/LengthofLongestSubstringWithoutRepetition.js @@ -0,0 +1,31 @@ +/** + * @function lengthOfLongestSubstring + * @description Finds the length of the longest substring in a given string without repeating characters + * @param {String} str - The input string + * @returns {Number} The Length of the longest substring in a given string without repeating characters + * @example lower("abcabcbb") => 3 + * @example lower("bbbbb") => 1 + */ + + +const lengthOfLongestSubstring = (s) => { + if (typeof s !== 'string') { + throw new TypeError('Invalid Input Type'); + } + + let maxLength = 0; + let start = 0; + const charMap = new Map(); + + for (let end = 0; end < s.length; end++) { + if (charMap.has(s[end])) { + start = Math.max(start, charMap.get(s[end]) + 1); + } + charMap.set(s[end], end); + maxLength = Math.max(maxLength, end - start + 1); + } + + return maxLength; + }; + +export {lengthOfLongestSubstring} \ No newline at end of file diff --git a/String/test/LengthofLongestSubstringWithoutRepetition.test.js b/String/test/LengthofLongestSubstringWithoutRepetition.test.js new file mode 100644 index 0000000000..8d52ae6d12 --- /dev/null +++ b/String/test/LengthofLongestSubstringWithoutRepetition.test.js @@ -0,0 +1,32 @@ +import { lengthOfLongestSubstring } from '../LengthofLongestSubstringWithoutRepetition'; + +describe('Testing the lengthOfLongestSubstring function', () => { + it('Test 1: Check by invalid type', () => { + expect(() => lengthOfLongestSubstring(345)).toThrowError(TypeError); + expect(() => lengthOfLongestSubstring(true)).toThrowError(TypeError); + expect(() => lengthOfLongestSubstring(null)).toThrowError(TypeError); + }); + + it('Test 2: Check with strings containing unique characters', () => { + expect(lengthOfLongestSubstring('abcabcbb')).toBe(3); + expect(lengthOfLongestSubstring('bbbbb')).toBe(1); + expect(lengthOfLongestSubstring('pwwkew')).toBe(3); + }); + + it('Test 3: Check with empty string', () => { + expect(lengthOfLongestSubstring('')).toBe(0); + }); + + it('Test 4: Check with string containing a single space', () => { + expect(lengthOfLongestSubstring(' ')).toBe(1); + }); + + it('Test 5: Check with mixed case string', () => { + expect(lengthOfLongestSubstring('AaBbCc')).toBe(3); + expect(lengthOfLongestSubstring('AbCdEf')).toBe(6); + }); + + it('Test 6: Check with long string with repeating characters', () => { + expect(lengthOfLongestSubstring('abcdefghijklmnaaaaa')).toBe(13); + }); +}); From d9c7e904e096eec9deb45d0d9c882f4845615489 Mon Sep 17 00:00:00 2001 From: Abhijit-033 Date: Sun, 1 Oct 2023 18:39:47 +0530 Subject: [PATCH 2/8] Fixed Errors and Mistakes in New Algorithm LengthofLongestSubstringWithoutRepetition --- ...ngthofLongestSubstringWithoutRepetition.js | 8 ++----- ...fLongestSubstringWithoutRepetition.test.js | 22 +++++++++---------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/String/LengthofLongestSubstringWithoutRepetition.js b/String/LengthofLongestSubstringWithoutRepetition.js index 5136fd3d3d..f724f45044 100644 --- a/String/LengthofLongestSubstringWithoutRepetition.js +++ b/String/LengthofLongestSubstringWithoutRepetition.js @@ -7,16 +7,13 @@ * @example lower("bbbbb") => 1 */ - const lengthOfLongestSubstring = (s) => { if (typeof s !== 'string') { throw new TypeError('Invalid Input Type'); } - let maxLength = 0; let start = 0; const charMap = new Map(); - for (let end = 0; end < s.length; end++) { if (charMap.has(s[end])) { start = Math.max(start, charMap.get(s[end]) + 1); @@ -24,8 +21,7 @@ const lengthOfLongestSubstring = (s) => { charMap.set(s[end], end); maxLength = Math.max(maxLength, end - start + 1); } - return maxLength; - }; - + } + export {lengthOfLongestSubstring} \ No newline at end of file diff --git a/String/test/LengthofLongestSubstringWithoutRepetition.test.js b/String/test/LengthofLongestSubstringWithoutRepetition.test.js index 8d52ae6d12..a0f2ed911b 100644 --- a/String/test/LengthofLongestSubstringWithoutRepetition.test.js +++ b/String/test/LengthofLongestSubstringWithoutRepetition.test.js @@ -2,31 +2,31 @@ import { lengthOfLongestSubstring } from '../LengthofLongestSubstringWithoutRepe describe('Testing the lengthOfLongestSubstring function', () => { it('Test 1: Check by invalid type', () => { - expect(() => lengthOfLongestSubstring(345)).toThrowError(TypeError); - expect(() => lengthOfLongestSubstring(true)).toThrowError(TypeError); - expect(() => lengthOfLongestSubstring(null)).toThrowError(TypeError); + expect(() => lengthOfLongestSubstring(345)).toThrowError(TypeError) + expect(() => lengthOfLongestSubstring(true)).toThrowError(TypeError) + expect(() => lengthOfLongestSubstring(null)).toThrowError(TypeError) }); it('Test 2: Check with strings containing unique characters', () => { - expect(lengthOfLongestSubstring('abcabcbb')).toBe(3); - expect(lengthOfLongestSubstring('bbbbb')).toBe(1); - expect(lengthOfLongestSubstring('pwwkew')).toBe(3); + expect(lengthOfLongestSubstring('abcabcbb')).toBe(3) + expect(lengthOfLongestSubstring('bbbbb')).toBe(1) + expect(lengthOfLongestSubstring('pwwkew')).toBe(3) }); it('Test 3: Check with empty string', () => { - expect(lengthOfLongestSubstring('')).toBe(0); + expect(lengthOfLongestSubstring('')).toBe(0) }); it('Test 4: Check with string containing a single space', () => { - expect(lengthOfLongestSubstring(' ')).toBe(1); + expect(lengthOfLongestSubstring(' ')).toBe(1) }); it('Test 5: Check with mixed case string', () => { - expect(lengthOfLongestSubstring('AaBbCc')).toBe(3); - expect(lengthOfLongestSubstring('AbCdEf')).toBe(6); + expect(lengthOfLongestSubstring('AaBbCc')).toBe(3) + expect(lengthOfLongestSubstring('AbCdEf')).toBe(6) }); it('Test 6: Check with long string with repeating characters', () => { - expect(lengthOfLongestSubstring('abcdefghijklmnaaaaa')).toBe(13); + expect(lengthOfLongestSubstring('abcdefghijklmnaaaaa')).toBe(13) }); }); From 78730998a61f704f857dd4ccbe08bee059ac8338 Mon Sep 17 00:00:00 2001 From: Abhijit-033 Date: Sun, 1 Oct 2023 18:43:47 +0530 Subject: [PATCH 3/8] fix:#1389 Errors Fixed --- .../LengthofLongestSubstringWithoutRepetition.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/String/LengthofLongestSubstringWithoutRepetition.js b/String/LengthofLongestSubstringWithoutRepetition.js index f724f45044..b5b09a3c54 100644 --- a/String/LengthofLongestSubstringWithoutRepetition.js +++ b/String/LengthofLongestSubstringWithoutRepetition.js @@ -9,19 +9,19 @@ const lengthOfLongestSubstring = (s) => { if (typeof s !== 'string') { - throw new TypeError('Invalid Input Type'); + throw new TypeError('Invalid Input Type') } - let maxLength = 0; - let start = 0; - const charMap = new Map(); + let maxLength = 0 + let start = 0 + const charMap = new Map() for (let end = 0; end < s.length; end++) { if (charMap.has(s[end])) { - start = Math.max(start, charMap.get(s[end]) + 1); + start = Math.max(start, charMap.get(s[end]) + 1) } - charMap.set(s[end], end); - maxLength = Math.max(maxLength, end - start + 1); + charMap.set(s[end], end) + maxLength = Math.max(maxLength, end - start + 1) } - return maxLength; + return maxLength } export {lengthOfLongestSubstring} \ No newline at end of file From 5e051ddb0e66258453e3a5ca5cbad349369b9ea5 Mon Sep 17 00:00:00 2001 From: Abhijit-033 Date: Sun, 1 Oct 2023 18:45:52 +0530 Subject: [PATCH 4/8] fix:#1389 Syntax and Code Errors Fixed --- ...thofLongestSubstringWithoutRepetition.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/String/test/LengthofLongestSubstringWithoutRepetition.test.js b/String/test/LengthofLongestSubstringWithoutRepetition.test.js index a0f2ed911b..9c715970af 100644 --- a/String/test/LengthofLongestSubstringWithoutRepetition.test.js +++ b/String/test/LengthofLongestSubstringWithoutRepetition.test.js @@ -1,32 +1,32 @@ -import { lengthOfLongestSubstring } from '../LengthofLongestSubstringWithoutRepetition'; +import { lengthOfLongestSubstring } from '../LengthofLongestSubstringWithoutRepetition' describe('Testing the lengthOfLongestSubstring function', () => { it('Test 1: Check by invalid type', () => { expect(() => lengthOfLongestSubstring(345)).toThrowError(TypeError) expect(() => lengthOfLongestSubstring(true)).toThrowError(TypeError) expect(() => lengthOfLongestSubstring(null)).toThrowError(TypeError) - }); + }) it('Test 2: Check with strings containing unique characters', () => { expect(lengthOfLongestSubstring('abcabcbb')).toBe(3) expect(lengthOfLongestSubstring('bbbbb')).toBe(1) expect(lengthOfLongestSubstring('pwwkew')).toBe(3) - }); + }) it('Test 3: Check with empty string', () => { expect(lengthOfLongestSubstring('')).toBe(0) - }); + }) it('Test 4: Check with string containing a single space', () => { expect(lengthOfLongestSubstring(' ')).toBe(1) - }); + }) it('Test 5: Check with mixed case string', () => { expect(lengthOfLongestSubstring('AaBbCc')).toBe(3) expect(lengthOfLongestSubstring('AbCdEf')).toBe(6) - }); + }) it('Test 6: Check with long string with repeating characters', () => { expect(lengthOfLongestSubstring('abcdefghijklmnaaaaa')).toBe(13) - }); -}); + }) +}) From b4d9d1d1ba7f9cd472ce116c7d14df23d7e1a318 Mon Sep 17 00:00:00 2001 From: Abhijit-033 Date: Sun, 1 Oct 2023 18:49:17 +0530 Subject: [PATCH 5/8] fix:#1389 Errors FIxed --- ...ngthofLongestSubstringWithoutRepetition.js | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/String/LengthofLongestSubstringWithoutRepetition.js b/String/LengthofLongestSubstringWithoutRepetition.js index b5b09a3c54..29a73bac4e 100644 --- a/String/LengthofLongestSubstringWithoutRepetition.js +++ b/String/LengthofLongestSubstringWithoutRepetition.js @@ -8,20 +8,20 @@ */ const lengthOfLongestSubstring = (s) => { - if (typeof s !== 'string') { - throw new TypeError('Invalid Input Type') - } - let maxLength = 0 - let start = 0 - const charMap = new Map() - for (let end = 0; end < s.length; end++) { - if (charMap.has(s[end])) { - start = Math.max(start, charMap.get(s[end]) + 1) - } - charMap.set(s[end], end) - maxLength = Math.max(maxLength, end - start + 1) + if (typeof s !== 'string') { + throw new TypeError('Invalid Input Type') + } + let maxLength = 0 + let start = 0 + const charMap = new Map() + for (let end = 0; end < s.length; end++) { + if (charMap.has(s[end])) { + start = Math.max(start, charMap.get(s[end]) + 1) } - return maxLength + charMap.set(s[end], end) + maxLength = Math.max(maxLength, end - start + 1) } + return maxLength +} -export {lengthOfLongestSubstring} \ No newline at end of file +export { lengthOfLongestSubstring } \ No newline at end of file From 1872f66c2cd41162f98902ca7edd4043c48d28c6 Mon Sep 17 00:00:00 2001 From: Abhijit-033 Date: Sun, 1 Oct 2023 18:50:57 +0530 Subject: [PATCH 6/8] Fixed All new errors --- String/LengthofLongestSubstringWithoutRepetition.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/LengthofLongestSubstringWithoutRepetition.js b/String/LengthofLongestSubstringWithoutRepetition.js index 29a73bac4e..2af7e60836 100644 --- a/String/LengthofLongestSubstringWithoutRepetition.js +++ b/String/LengthofLongestSubstringWithoutRepetition.js @@ -24,4 +24,4 @@ const lengthOfLongestSubstring = (s) => { return maxLength } -export { lengthOfLongestSubstring } \ No newline at end of file +export { lengthOfLongestSubstring } From 403bc146d765c995f9baec8ef59a5b801ee9a7d5 Mon Sep 17 00:00:00 2001 From: Abhijit-033 Date: Sun, 1 Oct 2023 23:55:06 +0530 Subject: [PATCH 7/8] fix:1389 Implemented suggestions and corrections --- ...ngthofLongestSubstringWithoutRepetition.js | 10 +++++----- ...fLongestSubstringWithoutRepetition.test.js | 20 +++++++------------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/String/LengthofLongestSubstringWithoutRepetition.js b/String/LengthofLongestSubstringWithoutRepetition.js index 2af7e60836..63cb997c67 100644 --- a/String/LengthofLongestSubstringWithoutRepetition.js +++ b/String/LengthofLongestSubstringWithoutRepetition.js @@ -1,10 +1,10 @@ -/** - * @function lengthOfLongestSubstring - * @description Finds the length of the longest substring in a given string without repeating characters +/* + * @description : Given a string, the function finds the length of the longest substring without any repeating characters * @param {String} str - The input string * @returns {Number} The Length of the longest substring in a given string without repeating characters - * @example lower("abcabcbb") => 3 - * @example lower("bbbbb") => 1 + * @example lengthOfLongestSubstring("abcabcbb") => 3 + * @example lengthOfLongestSubstring("bbbbb") => 1 + * More Info: https://leetcode.com/problems/longest-substring-without-repeating-characters/ */ const lengthOfLongestSubstring = (s) => { diff --git a/String/test/LengthofLongestSubstringWithoutRepetition.test.js b/String/test/LengthofLongestSubstringWithoutRepetition.test.js index 9c715970af..81e475ced4 100644 --- a/String/test/LengthofLongestSubstringWithoutRepetition.test.js +++ b/String/test/LengthofLongestSubstringWithoutRepetition.test.js @@ -1,32 +1,26 @@ import { lengthOfLongestSubstring } from '../LengthofLongestSubstringWithoutRepetition' -describe('Testing the lengthOfLongestSubstring function', () => { - it('Test 1: Check by invalid type', () => { +describe('LengthOfLongestSubstring', () => { + it('should throw error if parameter is not string', () => { expect(() => lengthOfLongestSubstring(345)).toThrowError(TypeError) expect(() => lengthOfLongestSubstring(true)).toThrowError(TypeError) expect(() => lengthOfLongestSubstring(null)).toThrowError(TypeError) }) - it('Test 2: Check with strings containing unique characters', () => { + it('should check substrings containing unique characters', () => { expect(lengthOfLongestSubstring('abcabcbb')).toBe(3) expect(lengthOfLongestSubstring('bbbbb')).toBe(1) expect(lengthOfLongestSubstring('pwwkew')).toBe(3) + expect(lengthOfLongestSubstring(' ')).toBe(1) + expect(lengthOfLongestSubstring('abcdefghijklmnaaaaa')).toBe(13) }) - it('Test 3: Check with empty string', () => { + it('should give zero for empty strings', () => { expect(lengthOfLongestSubstring('')).toBe(0) }) - it('Test 4: Check with string containing a single space', () => { - expect(lengthOfLongestSubstring(' ')).toBe(1) - }) - - it('Test 5: Check with mixed case string', () => { + it('should be case-sensitive', () => { expect(lengthOfLongestSubstring('AaBbCc')).toBe(3) expect(lengthOfLongestSubstring('AbCdEf')).toBe(6) }) - - it('Test 6: Check with long string with repeating characters', () => { - expect(lengthOfLongestSubstring('abcdefghijklmnaaaaa')).toBe(13) - }) }) From 2eabe735d680e6b71fb0d9f4f5ce8dc082ff6895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Mon, 2 Oct 2023 18:06:05 +0200 Subject: [PATCH 8/8] Use @see annotation --- String/LengthofLongestSubstringWithoutRepetition.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/LengthofLongestSubstringWithoutRepetition.js b/String/LengthofLongestSubstringWithoutRepetition.js index 63cb997c67..42b5894b7c 100644 --- a/String/LengthofLongestSubstringWithoutRepetition.js +++ b/String/LengthofLongestSubstringWithoutRepetition.js @@ -4,7 +4,7 @@ * @returns {Number} The Length of the longest substring in a given string without repeating characters * @example lengthOfLongestSubstring("abcabcbb") => 3 * @example lengthOfLongestSubstring("bbbbb") => 1 - * More Info: https://leetcode.com/problems/longest-substring-without-repeating-characters/ + * @see https://leetcode.com/problems/longest-substring-without-repeating-characters/ */ const lengthOfLongestSubstring = (s) => {