-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...ms-problem-solving/cracking-the-coding-interview-check-permutation/en/index.mdx
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,83 @@ | ||
<SmoothRender> | ||
|
||
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) | ||
|
||
--- | ||
|
||
</SmoothRender> |
23 changes: 23 additions & 0 deletions
23
...gorithms-problem-solving/cracking-the-coding-interview-check-permutation/en/metadata.json
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,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" | ||
} | ||
} |
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.