diff --git a/Interview-Preparation-Kit/Dictionary-and-Hashmap/sherlock-and-anagrams.js b/Interview-Preparation-Kit/Dictionary-and-Hashmap/sherlock-and-anagrams.js new file mode 100644 index 0000000..1c10a7d --- /dev/null +++ b/Interview-Preparation-Kit/Dictionary-and-Hashmap/sherlock-and-anagrams.js @@ -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; +}; diff --git a/README.md b/README.md index d0268dc..65b202f 100644 --- a/README.md +++ b/README.md @@ -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 | | --- | --- | --- |