From d3bc0623ad16222cdad2115262bf01994a407b4b Mon Sep 17 00:00:00 2001 From: anonimak Date: Fri, 7 Oct 2022 01:58:17 +0700 Subject: [PATCH 1/5] Added Happy Number --- LeetcodeProblems/Algorithms/Happy_Number.js | 49 +++++++++++++++++++ .../Algorithms/Happy_Number_Test.js | 9 ++++ README.md | 1 + 3 files changed, 59 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/Happy_Number.js create mode 100644 LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js diff --git a/LeetcodeProblems/Algorithms/Happy_Number.js b/LeetcodeProblems/Algorithms/Happy_Number.js new file mode 100644 index 0000000..e530761 --- /dev/null +++ b/LeetcodeProblems/Algorithms/Happy_Number.js @@ -0,0 +1,49 @@ +/** +Happy Number +https://leetcode.com/problems/happy-number/ + +Write an algorithm to determine if a number n is happy. + +A happy number is a number defined by the following process: + + Starting with any positive integer, replace the number by the sum of the squares of its digits. + Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. + Those numbers for which this process ends in 1 are happy. + +Return true if n is a happy number, and false if not. + +Example 1: +Input: n = 19 +Output: true +Explanation: +12 + 92 = 82 +82 + 22 = 68 +62 + 82 = 100 +12 + 02 + 02 = 1 + +Example 2: +Input: n = 2 +Output: false + + + */ + +/** + * @param {number} n + * @return {boolean} + */ +var isHappy = function(n) { + return checkHappyNumber(n); +}; + +function checkHappyNumber(n){ + strNumber = n.toString(); + splitNumber = strNumber.split(""); + if(splitNumber.length <= 1){ + return (n <= 1)? true:false; + } + const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0); + return checkHappyNumber(digit) +} + +module.exports.isHappy = isHappy; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js b/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js new file mode 100644 index 0000000..13d2013 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js @@ -0,0 +1,9 @@ +const assert = require('assert'); +const isHappy = require('../../LeetcodeProblems/Algorithms/Happy_Number').isHappy; + +var test = function () { + assert.equal(isHappy(19),true); + assert.equal(isHappy(2),false); +} + +module.exports.test = test; \ No newline at end of file diff --git a/README.md b/README.md index 07daedb..b8ee3c7 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ To run a specific problem in your console run `node ` (e.g. | [Permutations With Duplicates ](/LeetcodeProblems/Algorithms/Permutations_With_Duplicates.js) | | | | [Deletion Distance](/LeetcodeProblems/Algorithms/Deletion_Distance.js) | | | | [Award Budget Cuts](/LeetcodeProblems/Algorithms/Award_Budget_Cuts.js) | | | +| [Happy Number](https://leetcode.com/problems/happy-number/) | Easy | https://leetcode.com/problems/happy-number/ | ### Sorting Algorithms | Algoritmhs | From 74d4041af0869776718a497d429c820ed86d3e43 Mon Sep 17 00:00:00 2001 From: jonatan teofilus <48998369+anonimak@users.noreply.github.com> Date: Fri, 7 Oct 2022 10:38:17 +0700 Subject: [PATCH 2/5] update explanation square using ^ --- LeetcodeProblems/Algorithms/Happy_Number.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/LeetcodeProblems/Algorithms/Happy_Number.js b/LeetcodeProblems/Algorithms/Happy_Number.js index e530761..4d6bf3b 100644 --- a/LeetcodeProblems/Algorithms/Happy_Number.js +++ b/LeetcodeProblems/Algorithms/Happy_Number.js @@ -16,10 +16,10 @@ Example 1: Input: n = 19 Output: true Explanation: -12 + 92 = 82 -82 + 22 = 68 -62 + 82 = 100 -12 + 02 + 02 = 1 +1^2 + 9^2 = 82 +8^2 + 2^2 = 68 +6^2 + 8^2 = 100 +1^2 + 0^2 + 0^2 = 1 Example 2: Input: n = 2 @@ -46,4 +46,4 @@ function checkHappyNumber(n){ return checkHappyNumber(digit) } -module.exports.isHappy = isHappy; \ No newline at end of file +module.exports.isHappy = isHappy; From 7d77208497f1a0a54340443e0dbc2f63efa86faa Mon Sep 17 00:00:00 2001 From: anonimak Date: Fri, 7 Oct 2022 13:28:40 +0700 Subject: [PATCH 3/5] Added Shuffle_String problem solution --- LeetcodeProblems/Algorithms/Shuffle_String.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/Shuffle_String.js diff --git a/LeetcodeProblems/Algorithms/Shuffle_String.js b/LeetcodeProblems/Algorithms/Shuffle_String.js new file mode 100644 index 0000000..e0ccd21 --- /dev/null +++ b/LeetcodeProblems/Algorithms/Shuffle_String.js @@ -0,0 +1,33 @@ +/** + You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. + +Return the shuffled string. + + + +Example 1: + +Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] +Output: "leetcode" +Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. + +Example 2: + +Input: s = "abc", indices = [0,1,2] +Output: "abc" +Explanation: After shuffling, each character remains in its position. + + */ + +/** + * @param {string} s + * @param {number[]} indices + * @return {string} + */ +var restoreString = function(s, indices) { + let arrshuffle = []; + indices.forEach((sindex, index) => arrshuffle[sindex] = s.charAt(index)) + return arrshuffle.join('') +}; + +module.exports.restoreString = restoreString; \ No newline at end of file From d721505a22a6cee429abec56884af14d9a633286 Mon Sep 17 00:00:00 2001 From: anonimak Date: Fri, 7 Oct 2022 13:28:53 +0700 Subject: [PATCH 4/5] Added Shuffle_String problem test --- .../Algorithms/Shuffle_String_Test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js diff --git a/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js b/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js new file mode 100644 index 0000000..b53eef9 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js @@ -0,0 +1,10 @@ +const assert = require('assert'); +const restoreString = require('../../LeetcodeProblems/Algorithms/Shuffle_String').restoreString; + +var test = function () { + assert.equal(restoreString('codeleet',[4,5,6,7,0,2,1,3]),'leetcode'); + assert.equal(restoreString('abc',[0,1,2]),'abc'); + assert.equal(restoreString('hacktoberfest',[9,10,11,12,4,5,6,7,8,0,1,2,3]),'festtoberhack'); +} + +module.exports.test = test; \ No newline at end of file From 828779a49d220e65036183512a5d2d2c1a79af4a Mon Sep 17 00:00:00 2001 From: anonimak Date: Fri, 7 Oct 2022 13:29:13 +0700 Subject: [PATCH 5/5] Added Shuffle_String problem to the README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b8ee3c7..63fbbae 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ To run a specific problem in your console run `node ` (e.g. | [Deletion Distance](/LeetcodeProblems/Algorithms/Deletion_Distance.js) | | | | [Award Budget Cuts](/LeetcodeProblems/Algorithms/Award_Budget_Cuts.js) | | | | [Happy Number](https://leetcode.com/problems/happy-number/) | Easy | https://leetcode.com/problems/happy-number/ | +| [Shuffle String](https://leetcode.com/problems/shuffle-string/) | Easy | https://leetcode.com/problems/shuffle-string/ | ### Sorting Algorithms | Algoritmhs |