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/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/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 diff --git a/javascript/index.js b/javascript/index.js index dd5aafd..ab38037 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,50 @@ 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)); + +/** + * 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)); + + +/** + * 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