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 index 1ac29be9..da483385 100644 --- 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 @@ -8,15 +8,15 @@ Given two strings, write a method to decide if one is a permutation of the other ## Examples -```JavaScript -// input: 'aba', 'aab' -// output: true +```bash +input: 'aba', 'aab' +output: true -// input: 'aba', 'aaba' -// output: false +input: 'aba', 'aaba' +output: false -// input: 'aba', 'aa' -// output: false +input: 'aba', 'aa' +output: false ``` ## Solutions diff --git a/content/series/algorithms-problem-solving/cracking-the-coding-interview-is-unique/en/index.mdx b/content/series/algorithms-problem-solving/cracking-the-coding-interview-is-unique/en/index.mdx index fed7e5a1..c76e0abf 100644 --- a/content/series/algorithms-problem-solving/cracking-the-coding-interview-is-unique/en/index.mdx +++ b/content/series/algorithms-problem-solving/cracking-the-coding-interview-is-unique/en/index.mdx @@ -8,12 +8,12 @@ Is Unique: Implement an algorithm to determine if a string has all unique charac ## Examples -```JavaScript -// input: 'asdfghjkl' -// output: true +```bash +input: 'asdfghjkl' +output: true -// input: 'asdfghjkla' -// output: false +input: 'asdfghjkla' +output: false ``` ## Solutions diff --git a/content/series/algorithms-problem-solving/cracking-the-coding-interview-palindrome-permutation/en/index.mdx b/content/series/algorithms-problem-solving/cracking-the-coding-interview-palindrome-permutation/en/index.mdx new file mode 100644 index 00000000..6701445c --- /dev/null +++ b/content/series/algorithms-problem-solving/cracking-the-coding-interview-palindrome-permutation/en/index.mdx @@ -0,0 +1,89 @@ + + +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 a string, write a function to check if it is a permutation of a palindrome. +A palindrome is a word or phrase that is the same forwards and backwards. +A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. +You can ignore casing and non-letter characters. + +## Examples + +```bash +input: 'Tact Coa' +output: true + +input: 'tat' +output: true + +input: 'abcd' +output: false + +input: 'tactfcoa' +output: false + +input: 'tactfffcoa' +output: false +``` + +## Solution + +The first thing we do is to a hashmap of characters. For each character, we want to check the number of their occurances in the string. + +The character we add to the hashmaps are only alpha and downcased. So it's important to lower case the string and check if it is an alphabetic character. + +To check if the string is a palindrome, we just need to check the number of characters. If we have only one character that has an odd count, that can be a palindrome. Otherwise, it's not a palindrome and we can have a permutation for that. + +- Runtime Complexity: O(N), where N = the true length of the string +- Space Complexity: O(N), where N = the true length of the string in the hashmap + +```JavaScript +function isAlpha(char) { + return /[_a-zA-Z]/.test(char); +} + +function buildMapOsChars(string) { + const charsMap = new Map(); + const downcasedString = string.toLowerCase(); + + for (let char of downcasedString) { + if (!isAlpha(char)) continue; + if (charsMap.has(char)) charsMap.set(char, charsMap.get(char) + 1); + else charsMap.set(char, 1); + } + + return charsMap; +} + +function palindromePermutation(string) { + let charsMap = buildMapOsChars(string); + let numberOfCharsWithOneCount = 0; + + for (let [_, count] of charsMap.entries()) { + numberOfCharsWithOneCount += count % 2; + } + + return numberOfCharsWithOneCount <= 1; +} +``` + +## 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-palindrome-permutation/en/metadata.json b/content/series/algorithms-problem-solving/cracking-the-coding-interview-palindrome-permutation/en/metadata.json new file mode 100644 index 00000000..f4b6b9b6 --- /dev/null +++ b/content/series/algorithms-problem-solving/cracking-the-coding-interview-palindrome-permutation/en/metadata.json @@ -0,0 +1,23 @@ +{ + "title": "Cracking the Coding Interview: Palindrome Permutation", + "description": "Solving algorithmic problems, algorithmic techniques, and cracking the coding interview", + "date": "2023-09-27", + "tags": [ + { + "href": "/tags/javascript", + "name": "javascript" + }, + { + "href": "/tags/algorithms-and-data-structures", + "name": "algorithms_and_data_structures" + } + ], + "coverImage": { + "src": "/series/algorithms-problem-solving/palindrome-permutation.jpg", + "width": "640", + "height": "426", + "alt": "a wall covered in padlocks with a red double decker bus in the background", + "authorHref": "https://unsplash.com/@mayumimaciel", + "authorName": "Mayumi Maciel" + } +} diff --git a/content/series/algorithms-problem-solving/cracking-the-coding-interview-urlify/en/index.mdx b/content/series/algorithms-problem-solving/cracking-the-coding-interview-urlify/en/index.mdx index a42dd8b5..ceafa6f7 100644 --- a/content/series/algorithms-problem-solving/cracking-the-coding-interview-urlify/en/index.mdx +++ b/content/series/algorithms-problem-solving/cracking-the-coding-interview-urlify/en/index.mdx @@ -10,12 +10,12 @@ and that you are given the "true" length of the string ## Examples -```JavaScript -// input: 'Mr John Smith ', 13 -// output: 'Mr%20John%20Smith' +```bash +input: 'Mr John Smith ', 13 +output: 'Mr%20John%20Smith' -// input: 'Mr John Smith ', 13 -// output: 'Mr%20John%20Smith' +input: 'Mr John Smith ', 13 +output: 'Mr%20John%20Smith' ``` ## Solutions diff --git a/content/series/algorithms-problem-solving/en/index.mdx b/content/series/algorithms-problem-solving/en/index.mdx index bddca803..dd3b69c6 100644 --- a/content/series/algorithms-problem-solving/en/index.mdx +++ b/content/series/algorithms-problem-solving/en/index.mdx @@ -181,6 +181,11 @@ This is live document and will be updated everytime I solve new problems. title="Cracking the Coding Interview: URLify" url="/series/algorithms-problem-solving/cracking-the-coding-interview-urlify" /> + --- diff --git a/content/tags/algorithms-and-data-structures/en/index.mdx b/content/tags/algorithms-and-data-structures/en/index.mdx index a6881259..ac71edb3 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