Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @title Sherlock and Anagrams
* @difficulty Medium
* @link https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem
*/

const isAnagram = (origin, cand) => {
for (let i = 0; i < cand.length; i++) {
const del = origin.indexOf(cand.charAt(i));
if (del !== -1) {
origin = origin.substring(0, del) + origin.substring(del + 1);
} else {
return false;
}
}

return true;
};

const sherlockAndAnagrams = query => {
const {length} = query;
let cnt = 0;
for (let l = 1; l < length; l++) {
for (let start = 0; start + l < length; start++) {
const origin = query.substring(start, start + l);
for (let idx = start + 1; idx + l <= length; idx++) {
const cand = query.substring(idx, idx + l);
cnt = (isAnagram(origin, cand)) ? cnt + 1 : cnt;
}
}
}

return cnt;
};
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ Generates the README.md.
| --- | --- | --- |
| Easy | [2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem) | [Solution](./Interview-Preparation-Kit/Arrays/2d-array-ds.js)|
| Easy | [Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem) | [Solution](./Interview-Preparation-Kit/Arrays/left-rotation.js)|
#### Dictionary-and-Hashmap
| Difficulty | Problem | Solution |
| --- | --- | --- |
| Medium | [Sherlock and Anagrams](https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem) | [Solution](./Interview-Preparation-Kit/Dictionary-and-Hashmap/sherlock-and-anagrams.js)|
#### Strings
| Difficulty | Problem | Solution |
| --- | --- | --- |
#### Warm-up-Challenges
| Difficulty | Problem | Solution |
| --- | --- | --- |
Expand Down