|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert'; |
| 3 | +import { |
| 4 | + lengthOfLongestSubstring, |
| 5 | + lengthOfLongestSubstringSet, |
| 6 | + lengthOfLongestSubstringBruteForce, |
| 7 | +} from './solution'; |
| 8 | + |
| 9 | +describe('LeetCode 3 - Longest Substring Without Repeating Characters', () => { |
| 10 | + describe('lengthOfLongestSubstring (Sliding Window with Hash Map)', () => { |
| 11 | + it('should return 3 for s="abcabcbb"', () => { |
| 12 | + assert.strictEqual(lengthOfLongestSubstring('abcabcbb'), 3); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return 1 for s="bbbbb"', () => { |
| 16 | + assert.strictEqual(lengthOfLongestSubstring('bbbbb'), 1); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should return 3 for s="pwwkew"', () => { |
| 20 | + assert.strictEqual(lengthOfLongestSubstring('pwwkew'), 3); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should return 0 for empty string', () => { |
| 24 | + assert.strictEqual(lengthOfLongestSubstring(''), 0); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return 1 for single character', () => { |
| 28 | + assert.strictEqual(lengthOfLongestSubstring('a'), 1); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return length for all unique characters', () => { |
| 32 | + assert.strictEqual(lengthOfLongestSubstring('abcdef'), 6); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle string with spaces', () => { |
| 36 | + assert.strictEqual(lengthOfLongestSubstring('a b c'), 3); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should handle special characters', () => { |
| 40 | + assert.strictEqual(lengthOfLongestSubstring('!@#$%'), 5); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle numbers', () => { |
| 44 | + assert.strictEqual(lengthOfLongestSubstring('123121'), 3); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should handle mixed characters', () => { |
| 48 | + assert.strictEqual(lengthOfLongestSubstring('ab1!ab2@'), 6); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle duplicate at the end', () => { |
| 52 | + assert.strictEqual(lengthOfLongestSubstring('abcdea'), 5); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle alternating characters', () => { |
| 56 | + assert.strictEqual(lengthOfLongestSubstring('ababab'), 2); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should handle long unique substring in middle', () => { |
| 60 | + assert.strictEqual(lengthOfLongestSubstring('dvdf'), 3); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle two character string with duplicates', () => { |
| 64 | + assert.strictEqual(lengthOfLongestSubstring('au'), 2); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle complex case', () => { |
| 68 | + assert.strictEqual(lengthOfLongestSubstring('tmmzuxt'), 5); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('lengthOfLongestSubstringSet (Sliding Window with Set)', () => { |
| 73 | + it('should return 3 for s="abcabcbb"', () => { |
| 74 | + assert.strictEqual(lengthOfLongestSubstringSet('abcabcbb'), 3); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return 1 for s="bbbbb"', () => { |
| 78 | + assert.strictEqual(lengthOfLongestSubstringSet('bbbbb'), 1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should return 3 for s="pwwkew"', () => { |
| 82 | + assert.strictEqual(lengthOfLongestSubstringSet('pwwkew'), 3); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return 0 for empty string', () => { |
| 86 | + assert.strictEqual(lengthOfLongestSubstringSet(''), 0); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return 1 for single character', () => { |
| 90 | + assert.strictEqual(lengthOfLongestSubstringSet('x'), 1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should return length for all unique characters', () => { |
| 94 | + assert.strictEqual(lengthOfLongestSubstringSet('abcdefg'), 7); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should handle duplicate at the end', () => { |
| 98 | + assert.strictEqual(lengthOfLongestSubstringSet('abcdea'), 5); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle alternating characters', () => { |
| 102 | + assert.strictEqual(lengthOfLongestSubstringSet('ababab'), 2); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should handle complex case', () => { |
| 106 | + assert.strictEqual(lengthOfLongestSubstringSet('dvdf'), 3); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('lengthOfLongestSubstringBruteForce (Brute Force)', () => { |
| 111 | + it('should return 3 for s="abcabcbb"', () => { |
| 112 | + assert.strictEqual(lengthOfLongestSubstringBruteForce('abcabcbb'), 3); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should return 1 for s="bbbbb"', () => { |
| 116 | + assert.strictEqual(lengthOfLongestSubstringBruteForce('bbbbb'), 1); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should return 3 for s="pwwkew"', () => { |
| 120 | + assert.strictEqual(lengthOfLongestSubstringBruteForce('pwwkew'), 3); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should return 0 for empty string', () => { |
| 124 | + assert.strictEqual(lengthOfLongestSubstringBruteForce(''), 0); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should return 1 for single character', () => { |
| 128 | + assert.strictEqual(lengthOfLongestSubstringBruteForce('z'), 1); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should return length for all unique characters', () => { |
| 132 | + assert.strictEqual(lengthOfLongestSubstringBruteForce('abc'), 3); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should handle duplicate at the end', () => { |
| 136 | + assert.strictEqual(lengthOfLongestSubstringBruteForce('abcdea'), 5); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should handle alternating characters', () => { |
| 140 | + assert.strictEqual(lengthOfLongestSubstringBruteForce('aba'), 2); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle complex case', () => { |
| 144 | + assert.strictEqual(lengthOfLongestSubstringBruteForce('dvdf'), 3); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments