From c75b8f6de3623c7263cc44b26786873da78747bd Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Fri, 16 Oct 2020 21:28:23 +0100 Subject: Add nunovieira220 js solution to challenge 082 --- challenge-082/nunovieira220/js/ch-1.js | 13 ++++++++++++ challenge-082/nunovieira220/js/ch-2.js | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-082/nunovieira220/js/ch-1.js create mode 100644 challenge-082/nunovieira220/js/ch-2.js diff --git a/challenge-082/nunovieira220/js/ch-1.js b/challenge-082/nunovieira220/js/ch-1.js new file mode 100644 index 0000000000..6e5b2ef1cf --- /dev/null +++ b/challenge-082/nunovieira220/js/ch-1.js @@ -0,0 +1,13 @@ +// Input +const M = 12; +const N = 18; + +// Common Base String +const res = []; + +for (let i = 1; i < Math.min(M, N); i++) { + if(M % i === 0 && N % i == 0) res.push(i); +} + +// Output +console.log(res); \ No newline at end of file diff --git a/challenge-082/nunovieira220/js/ch-2.js b/challenge-082/nunovieira220/js/ch-2.js new file mode 100644 index 0000000000..67c8bf2f59 --- /dev/null +++ b/challenge-082/nunovieira220/js/ch-2.js @@ -0,0 +1,38 @@ +// Interleave +const interleave = (A, B, C) => { + if(A.length + B.length !== C.length) { + return 0; + } + + return interleaveChecker(A, B, C); +} + +// Interleave checker +const interleaveChecker = (A, B, C) => { + if(!C) return 1; + + const a = A.charAt(0); + const b = B.charAt(0); + const c = C.charAt(0); + let firstTree = 0; + let secondTree = 0; + + if(a === c) { + firstTree = interleaveChecker(A.substring(1), B, C.substring(1)); + } + + if(b === c) { + secondTree = interleaveChecker(A, B.substring(1), C.substring(1)) + } + + return firstTree + secondTree > 0 | 0; +} + +// Input +const A = 'XXY'; +const B = 'XXZ'; +const C = 'XXXXZY'; + +// Output +console.log(interleave(A, B, C)); + -- cgit