From e74a48239c3438b93ffb69996bbac53cda4b9eb2 Mon Sep 17 00:00:00 2001 From: TK Date: Mon, 25 Sep 2023 16:21:18 -0300 Subject: [PATCH] * --- .../en/index.mdx | 83 ++++++++++++++++++ .../en/metadata.json | 23 +++++ .../algorithms-problem-solving/en/index.mdx | 5 ++ .../en/index.mdx | 5 ++ .../check-permutation.jpg | Bin 0 -> 29395 bytes 5 files changed, 116 insertions(+) create mode 100644 content/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/index.mdx create mode 100644 content/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/metadata.json create mode 100644 public/series/algorithms-problem-solving/check-permutation.jpg diff --git a/content/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/index.mdx b/content/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/index.mdx new file mode 100644 index 00000000..1ac29be9 --- /dev/null +++ b/content/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/index.mdx @@ -0,0 +1,83 @@ + + +This post is part of the [`Algorithms Problem Solving`](/series/algorithms-problem-solving) series and a problem from the Cracking the Coding Interview book. + +## Problem description + +Given two strings, write a method to decide if one is a permutation of the other + +## Examples + +```JavaScript +// input: 'aba', 'aab' +// output: true + +// input: 'aba', 'aaba' +// output: false + +// input: 'aba', 'aa' +// output: false +``` + +## Solutions + +If they have different lengths, they are not permutations. + +Build a hashmap of characters of one of the strings +Iterate through the other string and check if the characters are in the hashmap +If there's no match in the hashmap, they are not permutations +If it passes the loop, they are permutations. + +- Runtime Complexity: O(N), where N = the length of the string +- Space Complexity: O(N), where N = the length of the string + +```JavaScript +function buildCharsMap(string) { + const charsMap = new Map(); + + for (let char of string) { + if (charsMap.has(char)) charsMap.set(char, charsMap.get(char) + 1); + else charsMap.set(char, 1); + } + + return charsMap; +} + +function checkPermutation(s1, s2) { + if (s1.length !== s2.length) return false; + const charsMap = buildCharsMap(s1); + + for (let char of s2) { + if (!charsMap.has(char)) { + return false; + } + + if (charsMap.get(char) === 0) { + return false; + } + + charsMap.set(char, charsMap.get(char) - 1); + } + + return true; +} +``` + +## Resources + +- [Algorithms Problem Solving Series](/series/algorithms-problem-solving) +- [Algorithms & Data Structures studies](https://github.com/imteekay/algorithms) +- [Is Unique source code](https://github.com/imteekay/algorithms/blob/master/coding_interviews/cracking-the-coding-interview/string/is-unique/is-unique.js) +- [Data Structures in JavaScript Series](/series/data-structures-in-javascript) +- [Stack Data Structure in JavaScript](/series/data-structures-in-javascript/stack-data-structure) +- [Queue Data Structure in JavaScript](/series/data-structures-in-javascript/queue-data-structure) +- [Linked List Data Structure in JavaScript](/series/data-structures-in-javascript/linked-list-data-structure) +- [Tree Data Structure in JavaScript](/series/data-structures-in-javascript/tree-data-structure) +- [Stack Data Structure](/series/data-structures/stack-data-structure) +- [Queue Data Structure](/series/data-structures/queue-data-structure) +- [Linked List](/series/data-structures/linked-list-data-structure) +- [Tree Data Structure](/series/data-structures/tree-data-structure) + +--- + + diff --git a/content/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/metadata.json b/content/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/metadata.json new file mode 100644 index 00000000..3b874086 --- /dev/null +++ b/content/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/metadata.json @@ -0,0 +1,23 @@ +{ + "title": "Cracking the Coding Interview: Check Permutation", + "description": "Solving algorithmic problems, algorithmic techniques, and cracking the coding interview", + "date": "2023-09-25", + "tags": [ + { + "href": "/tags/javascript", + "name": "javascript" + }, + { + "href": "/tags/algorithms-and-data-structures", + "name": "algorithms_and_data_structures" + } + ], + "coverImage": { + "src": "/series/algorithms-problem-solving/check-permutation.jpg", + "width": "640", + "height": "426", + "alt": "3x3 Rubik's Cube", + "authorHref": "https://unsplash.com/@jlxp", + "authorName": "Jean-Louis Paulin" + } +} diff --git a/content/series/algorithms-problem-solving/en/index.mdx b/content/series/algorithms-problem-solving/en/index.mdx index 739d94e3..ba76a0e2 100644 --- a/content/series/algorithms-problem-solving/en/index.mdx +++ b/content/series/algorithms-problem-solving/en/index.mdx @@ -171,6 +171,11 @@ This is live document and will be updated everytime I solve new problems. title="Cracking the Coding Interview: Is Unique" url="/series/algorithms-problem-solving/cracking-the-coding-interview-is-unique" /> + --- diff --git a/content/tags/algorithms-and-data-structures/en/index.mdx b/content/tags/algorithms-and-data-structures/en/index.mdx index ce096ee8..4c434ba8 100644 --- a/content/tags/algorithms-and-data-structures/en/index.mdx +++ b/content/tags/algorithms-and-data-structures/en/index.mdx @@ -3,6 +3,11 @@ ## 2023