-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add permutations, very slow solution atm
- Loading branch information
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Factorial: is the product of all positive integers less than or equal to n. | ||
* | ||
* So: | ||
* | ||
* 0! = 1 | ||
* 1! = 1 | ||
* 2! = 2x1 | ||
* 3! = 3x2x1 | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {number} n | ||
*/ | ||
export function factorial(n) { | ||
if (n < 0) { | ||
throw new Error("Invalid value for n") | ||
} | ||
|
||
if (n <= 0) { | ||
return 1 | ||
} | ||
|
||
let result = n | ||
|
||
for (let i = 1; i < n; i++) { | ||
result = result * (n - i) | ||
} | ||
|
||
return result | ||
} | ||
|
||
export function factorialRecursive(n) { | ||
if (n <= 1) { | ||
return 1 | ||
} | ||
|
||
return n * factorialRecursive(n - 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { it, describe } from "node:test" | ||
import assert from "node:assert" | ||
import { factorial, factorialRecursive } from "./factorial.mjs" | ||
|
||
describe("Calculate factorial", () => { | ||
describe("Iterative", () => { | ||
it("1!", () => { | ||
assert.deepStrictEqual(factorial(1), 1) | ||
}) | ||
it("0!", () => { | ||
assert.deepStrictEqual(factorial(0), 1) | ||
}) | ||
it("3!", () => { | ||
assert.deepStrictEqual(factorial(3), 6) | ||
}) | ||
it("4!", () => { | ||
assert.deepStrictEqual(factorial(4), 24) | ||
}) | ||
it("5!", () => { | ||
assert.deepStrictEqual(factorial(5), 120) | ||
}) | ||
}) | ||
|
||
describe("Recursive", () => { | ||
it("1!", () => { | ||
assert.deepStrictEqual(factorialRecursive(1), 1) | ||
}) | ||
it("0!", () => { | ||
assert.deepStrictEqual(factorialRecursive(0), 1) | ||
}) | ||
it("3!", () => { | ||
assert.deepStrictEqual(factorialRecursive(3), 6) | ||
}) | ||
it("4!", () => { | ||
assert.deepStrictEqual(factorialRecursive(4), 24) | ||
}) | ||
it("5!", () => { | ||
assert.deepStrictEqual(factorialRecursive(5), 120) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Exercise: https://leetcode.com/problems/permutation-in-string/description/ | ||
* | ||
* Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. | ||
In other words, return true if one of s1's permutations is the substring of s2. | ||
Example 1: | ||
Input: s1 = "ab", s2 = "eidbaooo" | ||
Output: true | ||
Explanation: s2 contains one permutation of s1 ("ba"). | ||
Example 2: | ||
Input: s1 = "ab", s2 = "eidboaoo" | ||
Output: false | ||
Constraints: | ||
1 <= s1.length, s2.length <= 104 | ||
s1 and s2 consist of lowercase English letters. | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {string} w | ||
* @param {Array.string} permutations | ||
* | ||
* @returns {Array.<string>} | ||
*/ | ||
export function getPermutations(str) { | ||
const results = [] | ||
|
||
if (str.length === 1) { | ||
results.push(str) | ||
return results | ||
} | ||
|
||
for (let i = 0; i < str.length; i++) { | ||
const firstChar = str[i] | ||
const charsLeft = [...str].filter((_, index) => index !== i).join("") | ||
const innerPermutations = getPermutations(charsLeft) | ||
for (let j = 0; j < innerPermutations.length; j++) { | ||
results.push(`${firstChar}${innerPermutations[j]}`) | ||
} | ||
} | ||
|
||
return results | ||
} | ||
|
||
export function containPermutation(s1, s2) { | ||
if (s1.length <= 0) { | ||
return false | ||
} | ||
if (s2.length >= 104) { | ||
return false | ||
} | ||
const permutations = getPermutations(s1) | ||
return permutations.some((w) => s2.indexOf(w) !== -1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { it, describe } from "node:test" | ||
|
||
import assert from "node:assert" | ||
|
||
import { | ||
getPermutations, | ||
containPermutation, | ||
} from "./permutation-in-string.mjs" | ||
|
||
describe("Permutations test suite", () => { | ||
it("Should calculate permutations for abc", () => { | ||
assert.deepStrictEqual(getPermutations("abc"), [ | ||
"abc", | ||
"acb", | ||
"bac", | ||
"bca", | ||
"cab", | ||
"cba", | ||
]) | ||
}) | ||
|
||
it("containPermutation ab", () => { | ||
assert.deepStrictEqual(containPermutation("ab", "eidbaooo"), true) | ||
}) | ||
|
||
it("containPermutation eidboaoo", () => { | ||
assert.deepStrictEqual(containPermutation("ab", "eidboaoo"), false) | ||
}) | ||
|
||
it("containPermutation prosperity", () => { | ||
assert.deepStrictEqual( | ||
containPermutation("prosperity", "properties"), | ||
false, | ||
) | ||
}) | ||
}) |