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 308b022

Browse files
Aditya1942raklaptudirm
andauthored
merge: Sliding window (#833)
* new algorithm 'Sliding-Window' added with example 'Longest Substring Without Repeating Characters' * new example of sliding window 'Permutation in String' added * chore: add `ignore_words_list` * sliding-window moved into Dynamic-programming directory Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com>
1 parent 12cf29e commit 308b022

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

‎.github/workflows/Codespell.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ jobs:
1010
with:
1111
# file types to ignore
1212
skip: "*.json,*.yml,DIRECTORY.md,PermutateString.test.js,SubsequenceRecursive.js"
13-
13+
ignore_words_list: "ba"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @name The-Sliding-Window Algorithm is primarily used for the problems dealing with linear data structures like Arrays, Lists, Strings etc.
3+
* These problems can easily be solved using Brute Force techniques which result in quadratic or exponential time complexity.
4+
* Sliding window technique reduces the required time to linear O(n).
5+
* @see [The-Sliding-Window](https://www.geeksforgeeks.org/window-sliding-technique/)
6+
*/
7+
8+
/**
9+
* @function LongestSubstringWithoutRepeatingCharacters
10+
* @description Get the length of the longest substring without repeating characters
11+
* @param {String} s - The input string
12+
*/
13+
export function LongestSubstringWithoutRepeatingCharacters (s) {
14+
let maxLength = 0
15+
let start = 0
16+
let end = 0
17+
const map = {}
18+
while (end < s.length) {
19+
if (map[s[end]] === undefined) {
20+
map[s[end]] = 1
21+
maxLength = Math.max(maxLength, end - start + 1)
22+
end++
23+
} else {
24+
while (s[start] !== s[end]) {
25+
delete map[s[start]]
26+
start++
27+
}
28+
delete map[s[start]]
29+
start++
30+
}
31+
}
32+
return maxLength
33+
}
34+
35+
// Example 1:
36+
// Input: s = "abcabcbb"
37+
// Output: 3
38+
// Explanation: The answer is "abc", with the length of 3.
39+
40+
// Example 2:
41+
// Input: s = "bbbbb"
42+
// Output: 1
43+
// Explanation: The answer is "b", with the length of 1.
44+
45+
// Example 3:
46+
// Input: s = "pwwkew"
47+
// Output: 3
48+
// Explanation: The answer is "wke", with the length of 3.
49+
// Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @name The-Sliding-Window Algorithm is primarily used for the problems dealing with linear data structures like Arrays, Lists, Strings etc.
3+
* These problems can easily be solved using Brute Force techniques which result in quadratic or exponential time complexity.
4+
* Sliding window technique reduces the required time to linear O(n).
5+
* @see [The-Sliding-Window](https://www.geeksforgeeks.org/window-sliding-technique/)
6+
*/
7+
/**
8+
* @function PermutationinString
9+
* @description Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
10+
* @param {String} s1 - The input string
11+
* @param {String} s2 - The input string
12+
* @return {boolean} - Returns true if s2 contains a permutation of s1, or false otherwise.
13+
*/
14+
15+
export function PermutationinString (s1, s2) {
16+
if (s1.length > s2.length) return false
17+
let start = 0
18+
let end = s1.length - 1
19+
const s1Set = SetHash()
20+
const s2Set = SetHash()
21+
for (let i = 0; i < s1.length; i++) {
22+
s1Set[s1[i]]++
23+
s2Set[s2[i]]++
24+
}
25+
if (equals(s1Set, s2Set)) return true
26+
while (end < s2.length - 1) {
27+
if (equals(s1Set, s2Set)) return true
28+
end++
29+
console.log(s2[start], s2[end], equals(s1Set, s2Set))
30+
const c1 = s2[start]
31+
const c2 = s2[end]
32+
if (s2Set[c1] > 0) s2Set[c1]--
33+
s2Set[c2]++
34+
start++
35+
if (equals(s1Set, s2Set)) return true
36+
}
37+
return false
38+
}
39+
function equals (a, b) {
40+
return JSON.stringify(a) === JSON.stringify(b)
41+
}
42+
43+
function SetHash () {
44+
const set = new Set()
45+
const alphabets = 'abcdefghijklmnopqrstuvwxyz'
46+
for (let i = 0; i < alphabets.length; i++) {
47+
set[alphabets[i]] = 0
48+
}
49+
return set
50+
}
51+
52+
// Example 1:
53+
// Input: s1 = "ab", s2 = "eidbaooo"
54+
// Output: true
55+
// Explanation: s2 contains one permutation of s1 ("ba").
56+
57+
// Example 2:
58+
// Input: s1 = "ab", s2 = "eidboaoo"
59+
// Output: false
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { LongestSubstringWithoutRepeatingCharacters } from '../LongestSubstringWithoutRepeatingCharacters.js'
2+
3+
describe('LongestSubstringWithoutRepeatingCharacters', () => {
4+
it('should return longest substring without repeating characters', () => {
5+
expect(LongestSubstringWithoutRepeatingCharacters('abcabcbb')).toEqual(3)
6+
expect(LongestSubstringWithoutRepeatingCharacters('bbbbb')).toEqual(1)
7+
expect(LongestSubstringWithoutRepeatingCharacters('pwwkew')).toEqual(3)
8+
expect(LongestSubstringWithoutRepeatingCharacters('a')).toEqual(1)
9+
expect(LongestSubstringWithoutRepeatingCharacters('')).toEqual(0)
10+
})
11+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PermutationinString } from '../PermutationinString.js'
2+
3+
describe('PermutationinString', () => {
4+
it("should return true if one of s1's permutations is the substring of s2", () => {
5+
expect(PermutationinString('ab', 'eidbaooo')).toEqual(true)
6+
expect(PermutationinString('abc', 'bcab')).toEqual(true)
7+
expect(PermutationinString('ab', 'eidboaoo')).toEqual(false)
8+
expect(PermutationinString('abc', '')).toEqual(false)
9+
})
10+
})

0 commit comments

Comments
(0)

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