Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Sep 25, 2023
1 parent c1610c8 commit e74a482
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
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>
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"
}
}
5 changes: 5 additions & 0 deletions content/series/algorithms-problem-solving/en/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>
<PostAndDate
date="2023-09-25"
title="Cracking the Coding Interview: Check Permutation"
url="/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation"
/>
</ul>

---
Expand Down
5 changes: 5 additions & 0 deletions content/tags/algorithms-and-data-structures/en/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
## 2023

<ul>
<PostAndDate
date="2023-09-25"
title="Cracking the Coding Interview: Check Permutation"
url="/series/algorithms-problem-solving/cracking-the-coding-interview-check-permutation"
/>
<PostAndDate
date="2023-09-24"
title="Cracking the Coding Interview: Is Unique"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e74a482

Please sign in to comment.