Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Sep 27, 2023
1 parent be4740a commit dc99c29
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<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 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)

---

</SmoothRender>
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 @@ -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"
/>
<PostAndDate
date="2023-09-27"
title="Cracking the Coding Interview: Palindrome Permutation"
url="/series/algorithms-problem-solving/cracking-the-coding-interview-palindrome-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-27"
title="Cracking the Coding Interview: Palindrome Permutation"
url="/series/algorithms-problem-solving/cracking-the-coding-interview-palindrome-permutation"
/>
<PostAndDate
date="2023-09-26"
title="Cracking the Coding Interview: URLify"
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 dc99c29

Please sign in to comment.