From 4cd06a5d2660c1940f5c71a6dec74924d05db4db Mon Sep 17 00:00:00 2001 From: Mavis Date: Mon, 27 Apr 2026 14:14:39 +0800 Subject: [PATCH 1/4] add 804 --- javascript/LeetCode/Array/804.js | 34 ++++++++++++++++++++++++++++++++ javascript/index.js | 5 +++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 javascript/LeetCode/Array/804.js diff --git a/javascript/LeetCode/Array/804.js b/javascript/LeetCode/Array/804.js new file mode 100644 index 0000000..16caf39 --- /dev/null +++ b/javascript/LeetCode/Array/804.js @@ -0,0 +1,34 @@ +/** + * 804. Unique Morse Code Words + * + * 給一個字串陣列作為參數,把該陣列內的元素一一拆開成字元,去比對每個字元轉換成摩斯密碼後共有幾個不同的 + * + * 英文字母是小寫 + * @param {string[]} words + * @return {number} + */ +var uniqueMorseRepresentations = function(words) { + // 26英文字母分別代表的morse code + let morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]; + + let set = new Set(); + for(const letter of words){ + let combine = ""; + for(const a of letter) { + combine += morse[a.charCodeAt(0) - 'a'.charCodeAt(0)]; + } + set.add(combine); + } + return set.size; +}; +let words = ["gin","zen","gig","msg"] +/* +Output: 2 +Explanation: The transformation of each word is: +"gin" -> "--...-." +"zen" -> "--...-." +"gig" -> "--...--." +"msg" -> "--...--." +There are 2 different transformations: "--...-." and "--...--.". +*/ +console.log(uniqueMorseRepresentations(words)); \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index dd5aafd..d0ba91c 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -836,7 +836,7 @@ var longestPalindrome = function(words) { }; -let words = ["ab","ty","yt","lc","cl","ab"]; +// let words = ["ab","ty","yt","lc","cl","ab"]; // 8 // "ty" + "lc" + "cl" + "yt" = "tylc clyt" // abtyytba @@ -1383,4 +1383,5 @@ Query 0: The element at queries[0] = 0 is nums[0] = 1. The nearest index with th Query 1: The element at queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1. Query 2: The element at queries[2] = 5 is nums[5] = 3. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: 5 -> 6 -> 0 -> 1). */ -// console.log(solveQueries(nums,queries)); \ No newline at end of file +// console.log(solveQueries(nums,queries)); + From db40c1ab72cd9a2cbb19e413e8369377baaae943 Mon Sep 17 00:00:00 2001 From: Mavis Date: Wed, 29 Apr 2026 14:15:49 +0800 Subject: [PATCH 2/4] practice --- javascript/index.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/javascript/index.js b/javascript/index.js index d0ba91c..7be678a 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1385,3 +1385,28 @@ Query 2: The element at queries[2] = 5 is nums[5] = 3. The nearest index with th */ // console.log(solveQueries(nums,queries)); +/** + * 645. Set Mismatch + * + * 參數為數字陣列,從1至n,但內有重複的元素,找出它們並調整成對的元素 + * + * @param {number[]} nums + * @return {number[]} + */ +var findErrorNums = function(nums) { + let res = []; + let map = new Map(); + for(const ele of nums){ + map.has(ele) ? map.set(ele,map.get(ele) + 1 ) : map.set(ele,1); + } + // console.log(map) + for(const [key,value] of map) { + if(value >= 2){ + console.log(key); + + } + } +}; +let nums = [1,2,2,4]; +// [2,3] +console.log(findErrorNums(nums)); \ No newline at end of file From 7dbba0775cd3bc930ff61167bb3ed0bae210ae5e Mon Sep 17 00:00:00 2001 From: Mavis Date: Thu, 30 Apr 2026 14:12:38 +0800 Subject: [PATCH 3/4] add no.3340 --- javascript/LeetCode/String/3340.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 javascript/LeetCode/String/3340.js diff --git a/javascript/LeetCode/String/3340.js b/javascript/LeetCode/String/3340.js new file mode 100644 index 0000000..6d74e45 --- /dev/null +++ b/javascript/LeetCode/String/3340.js @@ -0,0 +1,23 @@ +/** + * 3340. Check Balanced String + * + * 參數為字串數字,檢查index 偶數和基數各自相加後是否相等。 + * + * @param {string} num + * @return {boolean} + */ +var isBalanced = function(num) { + // solution + // TC:O(N) + let oddSum = 0, evenSum = 0; + for(let i = 0;i < num.length;++i) { + if(i % 2 === 0){ + evenSum += parseInt(num[i]); + }else{ + oddSum += parseInt(num[i]); + } + } + return evenSum === oddSum; +}; +let num = "1234"; +console.log(isBalanced(num)); \ No newline at end of file From f44964b23c56fc1bb8032d28bf0e887e31218ea5 Mon Sep 17 00:00:00 2001 From: Mavis Date: Mon, 4 May 2026 13:31:13 +0800 Subject: [PATCH 4/4] add 1967 --- javascript/LeetCode/Array/1967.js | 31 +++++++++++++++++++++++++++++++ javascript/index.js | 22 +++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 javascript/LeetCode/Array/1967.js diff --git a/javascript/LeetCode/Array/1967.js b/javascript/LeetCode/Array/1967.js new file mode 100644 index 0000000..3ccea96 --- /dev/null +++ b/javascript/LeetCode/Array/1967.js @@ -0,0 +1,31 @@ +/** + * 1967. Number of Strings That Appear as Substrings in Word + * + * @param {string[]} patterns + * @param {string} word + * @return {number} + */ +var numOfStrings = function(patterns, word) { + /** + * 計算patterns中有幾個元素出現在word中 + */ + let count = 0; + for(let i = 0;i < patterns.length;++i) { + if(word.includes(patterns[i])){ + count++; + } + } + return count; +}; +let patterns = ["a","abc","bc","d"], word = "abc"; +/* +Output: 3 +Explanation: +- "a" appears as a substring in "abc". +- "abc" appears as a substring in "abc". +- "bc" appears as a substring in "abc". +- "d" does not appear as a substring in "abc". +3 of the strings in patterns appear as a substring in word. +*/ +console.log(numOfStrings(patterns,word)); + diff --git a/javascript/index.js b/javascript/index.js index 7be678a..ab38037 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1409,4 +1409,24 @@ var findErrorNums = function(nums) { }; let nums = [1,2,2,4]; // [2,3] -console.log(findErrorNums(nums)); \ No newline at end of file +// console.log(findErrorNums(nums)); + + +/** + * 2864. Maximum Odd Binary Number + * + * @param {string} s + * @return {string} + */ +var maximumOddBinaryNumber = function(s) { + /** + * binary string s包含至少一個'1' + * rearrange 參數s成最大的奇二進制數 + * 回傳的結果值開頭可以是0 + */ + +}; +let s = "010"; +// Output: "001" +// Explanation: Because there is just one '1', it must be in the last position. So the answer is "001". +console.log(maximumOddBinaryNumber(s)); \ No newline at end of file