From 95d422fef6f82ee27e3a9124cac69428d1ff40b7 Mon Sep 17 00:00:00 2001 From: dmswl6310 Date: Tue, 29 Sep 2026 22:40:20 +0900 Subject: [PATCH] 0929 --- .../242. Valid Anagram.js" | 20 ++++++++++ .../413. Arithmetic Slices.js" | 39 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 "leetcode3/\355\231\251\354\235\200\354\247\200/242. Valid Anagram.js" create mode 100644 "leetcode3/\355\231\251\354\235\200\354\247\200/413. Arithmetic Slices.js" diff --git "a/leetcode3/\355\231\251\354\235\200\354\247\200/242. Valid Anagram.js" "b/leetcode3/\355\231\251\354\235\200\354\247\200/242. Valid Anagram.js" new file mode 100644 index 00000000..a192ef93 --- /dev/null +++ "b/leetcode3/\355\231\251\354\235\200\354\247\200/242. Valid Anagram.js" @@ -0,0 +1,20 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + if (s.length !== t.length) return false; + const countMap = new Map(); + + for (const char of s) { + countMap.set(char, (countMap.get(char) || 0) + 1); + } + + for (const char of t) { + const count = countMap.get(char); + if (count === 0 || count === undefined) return false; + countMap.set(char, count - 1); + } + return true; +}; diff --git "a/leetcode3/\355\231\251\354\235\200\354\247\200/413. Arithmetic Slices.js" "b/leetcode3/\355\231\251\354\235\200\354\247\200/413. Arithmetic Slices.js" new file mode 100644 index 00000000..35ce3a8b --- /dev/null +++ "b/leetcode3/\355\231\251\354\235\200\354\247\200/413. Arithmetic Slices.js" @@ -0,0 +1,39 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var numberOfArithmeticSlices = function (nums) { + if (nums.length < 3) return 0; + const diff = Array(nums.length - 1); + let count = 0; + + for (let i = 0; i < nums.length - 1; i++) { + diff[i] = nums[i + 1] - nums[i]; + } + + let prevDiff = diff[0]; + let startIndex = 0; + for (let i = 1; i < diff.length; i++) { + if (diff[i] !== prevDiff) { + if (i - 1 !== startIndex) { + const sameCount = i - startIndex; + let plus = sameCount - 2 + 1; + while (plus > 0) { + count += plus--; + } + } + startIndex = i; + prevDiff = diff[i]; + } + } + + if (startIndex !== diff.length - 1) { + const sameCount = diff.length - startIndex; + let plus = sameCount - 2 + 1; + while (plus > 0) { + count += plus--; + } + } + + return count; +};