From 6388734809577c33f7667542a513171fd456155c Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Tue, 13 Aug 2019 23:13:48 +0900 Subject: [PATCH] =?UTF-8?q?resolve=20#134-common-child=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=ED=92=80=EC=9D=B4=20=EB=B0=8F=20=EB=AC=B8=EC=84=9C?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../String-Manipulation/common-child.js | 35 +++++++++++++++++++ README.md | 7 ++++ 2 files changed, 42 insertions(+) create mode 100644 Interview-Preparation-Kit/String-Manipulation/common-child.js diff --git a/Interview-Preparation-Kit/String-Manipulation/common-child.js b/Interview-Preparation-Kit/String-Manipulation/common-child.js new file mode 100644 index 0000000..a4f030e --- /dev/null +++ b/Interview-Preparation-Kit/String-Manipulation/common-child.js @@ -0,0 +1,35 @@ +/** + * @title Common Child + * @difficulty Medium + * @link https://www.hackerrank.com/challenges/common-child/problem + */ + +const lca = (string1, string2) => { + const s1 = (string1.length <= string2.length) ? [...string1] : [...string2]; + const s2 = (string1.length <= string2.length) ? [...string2] : [...string1]; + const {length: rowLength} = s1; + const {length: colLength} = s2; + let row = s1.reduce(acc => { + acc.push(0); + + return acc; + }, []); + row.push(0); + + const cur = [...row]; + + for (let idxS2 = 0; idxS2 < colLength; idxS2++) { + for (let idxS1 = 0; idxS1 < rowLength; idxS1++) { + if (s1[idxS1] === s2[idxS2]) { + cur[idxS1 + 1] = row[idxS1] + 1; + } else { + cur[idxS1 + 1] = (Math.max(cur[idxS1], row[idxS1 + 1])); + } + } + row = [...cur]; + } + + return cur[rowLength]; +}; + +const commonChild = (s1, s2) => lca(s1, s2); diff --git a/README.md b/README.md index e1e1f85..6bf0be6 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,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)| +#### String-Manipulation +| Difficulty | Problem | Solution | +| --- | --- | --- | +| Medium | [Common Child](https://www.hackerrank.com/challenges/common-child/problem) | [Solution](./Interview-Preparation-Kit/String-Manipulation/common-child.js)| +#### Strings +| Difficulty | Problem | Solution | +| --- | --- | --- | #### Warm-up-Challenges | Difficulty | Problem | Solution | | --- | --- | --- |