From f139ab9c7a483634396d39200ef68194302419f7 Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Mon, 5 Aug 2019 22:00:07 +0900 Subject: [PATCH 1/2] =?UTF-8?q?resolve=20#116-sherlock-and-anagrams=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=ED=92=80=EC=9D=B4=20=EB=B0=8F=20=EB=AC=B8?= =?UTF-8?q?=EC=84=9C=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Strings/sherlock-and-anagrams.js | 34 +++++++++++++++++++ README.md | 4 +++ 2 files changed, 38 insertions(+) create mode 100644 Interview-Preparation-Kit/Strings/sherlock-and-anagrams.js diff --git a/Interview-Preparation-Kit/Strings/sherlock-and-anagrams.js b/Interview-Preparation-Kit/Strings/sherlock-and-anagrams.js new file mode 100644 index 0000000..1c10a7d --- /dev/null +++ b/Interview-Preparation-Kit/Strings/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..c6915f2 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,10 @@ 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)| +#### Strings +| Difficulty | Problem | Solution | +| --- | --- | --- | +| Medium | [Sherlock and Anagrams](https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem) | [Solution](./Interview-Preparation-Kit/Strings/sherlock-and-anagrams.js)| #### Warm-up-Challenges | Difficulty | Problem | Solution | | --- | --- | --- | From 4e4ecde0962937d1e5b3b754701ea6dd1f997f91 Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Wed, 7 Aug 2019 22:12:35 +0900 Subject: [PATCH 2/2] =?UTF-8?q?chore=20#117-=ED=8C=8C=EC=9D=BC=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99=20=EB=B0=8F=20=EB=AC=B8=EC=84=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sherlock-and-anagrams.js | 0 README.md | 5 ++++- 2 files changed, 4 insertions(+), 1 deletion(-) rename Interview-Preparation-Kit/{Strings => Dictionary-and-Hashmap}/sherlock-and-anagrams.js (100%) diff --git a/Interview-Preparation-Kit/Strings/sherlock-and-anagrams.js b/Interview-Preparation-Kit/Dictionary-and-Hashmap/sherlock-and-anagrams.js similarity index 100% rename from Interview-Preparation-Kit/Strings/sherlock-and-anagrams.js rename to Interview-Preparation-Kit/Dictionary-and-Hashmap/sherlock-and-anagrams.js diff --git a/README.md b/README.md index c6915f2..65b202f 100644 --- a/README.md +++ b/README.md @@ -61,10 +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 | | --- | --- | --- | -| Medium | [Sherlock and Anagrams](https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem) | [Solution](./Interview-Preparation-Kit/Strings/sherlock-and-anagrams.js)| #### Warm-up-Challenges | Difficulty | Problem | Solution | | --- | --- | --- |