diff --git a/README.md b/README.md index ab4a28d..2646ab3 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# practice +# leetcode_javascript_and_python +

目的

-1. 主要用來練習JS,和Python +1. 主要用來練習JavaScript和Python 2. 題目來源: - LeetCode - CodeWars - HackerRank -雖是JavaScript,但實際上使用Node.js,因此不需要打開瀏覽器便可執行JS的環境 -CMD打上node {檔案名稱.js},EG.node index.js
-而Python,則是打上 python3 {檔案名稱.py} EG.python3 index.py \ No newline at end of file +使用Docker對應Image和腳本來執行 +JavaScript 使用node diff --git a/javascript/LeetCode/Array/2442.js b/javascript/LeetCode/Array/2442.js new file mode 100644 index 0000000..5b93fff --- /dev/null +++ b/javascript/LeetCode/Array/2442.js @@ -0,0 +1,23 @@ +/** + * 2442. Count Number of Distinct Integers After Reverse Operations + * + * 計算陣列元素digits反轉後,加上原有陣列會有幾個數字是唯一值 + * + * @param {number[]} nums + * @return {number} + */ +var countDistinctIntegers = function(nums) { + // O(N) + nums.push(...nums.map(num => + parseInt(num.toString().split('').reverse().join('')) + )); + return new Set(nums).size; +}; +let nums = [1,13,10,12,31]; +/* +Output: 6 +Explanation: After including the reverse of each number, the resulting array is [1,13,10,12,31,1,31,1,21,13]. +The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1. +The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31). +*/ +console.log(countDistinctIntegers(nums)); \ No newline at end of file diff --git a/javascript/LeetCode/String/1653.js b/javascript/LeetCode/String/1653.js new file mode 100644 index 0000000..ace1096 --- /dev/null +++ b/javascript/LeetCode/String/1653.js @@ -0,0 +1,47 @@ +/** + * 1653. Minimum Deletions to Make String Balanced + * + * 參數s中只有'a' & 'b'這兩個字母。 + * 刪除任一字母使s balanced,若不存在一對index (i,j) 使得 i < j 且 s[i] = 'b' 且 s[j] = 'a',則s 是balanced。 + * 回傳至少須刪除幾次(操作幾次)才能使s balanced + * + * + * @param {string} s + * @return {number} + */ +var minimumDeletions = function(s) { + // balanced string中,b不能出現在a之後 + // no such 'b' at s[i] where s[j] is 'a' and i < j + + // TC:O(N) + // 計算a,b各自出現幾次 + let countA = 0,countB = 0; + let minDel = s.length; + // 先計算a出現幾次 + for(let i = 0;i < s.length;++i) { + if(s[i] === "a"){ + countA++; + } + } + // 之後再次迴圈,若遇到a則-- + for(let i = 0; i < s.length;++i) { + if(s[i] === "a"){ + countA--; + } + // 不斷更新比較雙方次數 + minDel = Math.min(minDel,countA + countB); + + // 遇到b,++ + if(s[i] === "b"){ + countB++; + } + } + return minDel; +}; +let s = "aababbab"; +/*Output: 2 +Explanation: You can either: +Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or +Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb"). +*/ +console.log(minimumDeletions(s)); diff --git a/javascript/LeetCode/String/3760.js b/javascript/LeetCode/String/3760.js new file mode 100644 index 0000000..e4813ba --- /dev/null +++ b/javascript/LeetCode/String/3760.js @@ -0,0 +1,31 @@ +/** + * 3760. Maximum Substrings With Distinct Start + * Difficulty:Medium + * + * @param {string} s + * @return {number} + */ +var maxDistinct = function(s) { + // 計算字串中,若每個開頭是跟另一substring開頭不同的字母,可以有幾種組合 + // 計算每個字母出現次數 + + // let map = new Map(); + // for(let i = 0; i < s.length;++i) { + // map = map.has(s[i]) ? map.set(s[i], map.get(s[i]) + 1) : map.set(s[i], 1); + // } + // return map.size; + + // solution 2. + /** + * TC: O(N) => + * 將s弄成陣列,須loop所有元素,因此O(N) + * new Set(...) 將每個元素插入set,add是O(1)但要做n次,因此O(N) + * size 讀取長度,因此O(1) + * + * new Set([...s]) 需要loop並插入所有元素,所以整體是O(n) + * */ + return new Set([...s]).size; +}; +let s = "abab"; +// 2 +console.log(maxDistinct(s)) \ No newline at end of file diff --git a/javascript/LeetCode/String/3884.js b/javascript/LeetCode/String/3884.js new file mode 100644 index 0000000..21be1c9 --- /dev/null +++ b/javascript/LeetCode/String/3884.js @@ -0,0 +1,26 @@ +/** + * 3884. First Matching Character From Both Ends + * + * Return the smallest index i such that s[i] == s[s.length - i - 1]. + * 找出最小index,須符合s[i] === s[s.length - i - 1]這條件,若沒有則-1 + * + * @param {string} s + * @return {number} + */ +var firstMatchingIndex = function(s) { + // TC: O(N) + // SC: O(1) + let i = 0,j = s.length - 1; + while(i <= j){ + if(s[i] === s[j]){ + // 左邊index一定是最小的 + return i; + } + i++; // 左邊index ++ + j--; // 右邊index -- + } + return -1; +}; +let s = "abcacbd"; +// 1 +console.log(firstMatchingIndex(s)); \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index f11eb90..db0c900 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1,9 +1,7 @@ // debugger -import { format } from 'node:path'; import {ExecutionTimer} from './time.js'; import assert from 'node:assert/strict'; import { count } from 'node:console'; -import { lchown } from 'node:fs'; /* 22. Generate Parentheses @@ -108,7 +106,7 @@ var findLongestWord = function (s, dictionary) { * Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. * Note: You must not use any built-in BigInteger library or convert the inputs to integer directly. * - * Input num1 and num2 are 非負數以字串方式呈現 + * Input num1 and num2 非負數以字串方式呈現 * Output num1 * num2(以字串方式呈現) * 不能使用內建含式或直接把Input轉成數字 * ------------------------------------------- @@ -131,21 +129,23 @@ var findLongestWord = function (s, dictionary) { * @return {string} */ var multiply = function (num1, num2) { + /** + * 不能使用內建涵式或轉換型態 + */ - let pattern = /^[0-9]+$/; - - if (!num1.match(pattern) || !num2.match(pattern) || Number(num1) === 0 || Number(num2) === 0) { - return; + let answer = Array(num1.length + num2.length).fill(0); + console.log(answer) + for(let i = num1.length - 1;i >= 0;i--){ + } - - - }; // const num1 = "2", num2 = "3"; // "6" // const num1 = "123", num2 = "456"; // "56088" -// console.log(multiply(num1, num2)); +const num1 = "123456789",num2 = "987654321"; +// "121932631112635269" +// console.log(multiply(num1, num2));ㄋㄋ @@ -1258,24 +1258,61 @@ var minRemoval = function(nums, k) { // console.log(minRemoval(nums,k)); -/** - * 1653. Minimum Deletions to Make String Balanced +/*** + * 890. Find and Replace Pattern * - * 參數s中只有'a' & 'b'這兩個字母。 - * 刪除任一字母使s balanced,若不存在一對index (i,j) 使得 i < j 且 s[i] = 'b' 且 s[j] = 'a',則s 是balanced。 - * 回傳最小須刪除幾次才能使s balanced - * - * @param {string} s - * @return {number} + * @param {string[]} words + * @param {string} pattern + * @return {string[]} */ -var minimumDeletions = function(s) { - -}; -// let s = "aababbab"; -/*Output: 2 -Explanation: You can either: -Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or -Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb"). -*/ -// console.log(minimumDeletions(s)); +var findAndReplacePattern = function(words, pattern) { + // solution 1. + // TC: O(n * m) + // let result = []; + // for(let i = 0;i < words.length;i++) { + // if(checkEqual(words[i],pattern)){ + // result.push(words[i]); + // } + // } + // return result; + + // /** + // * @param {string} a + // * @param {string} b + // * @return {boolean} + // */ + // function checkEqual(a,b) { + // for(let i = 0;i < a.length;i++) { + // if(a.indexOf(a[i]) !== b.indexOf(b[i])){ + // return false; + // } + // } + // return true; + // } + + // solution 2. + // hash map + let result = []; + for(const a of words) { + if(checkEqual(a,pattern)){ + result.push(a); + } + // console.log(a) + } + + function checkEqual(a,b){ + let map = new Map(); + for(let i = 0;i < a.length;++i) { + if(!map.has(a[i])){ + map.set(i,a[i]); + } + if(map.get(a[i]) ){ + + } + } + } +}; +let word = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"; +// ["mee","aqq"] +// console.log(findAndReplacePattern(word,pattern)); \ No newline at end of file