diff --git a/solution/0200-0299/0219.Contains Duplicate II/Solution.js b/solution/0200-0299/0219.Contains Duplicate II/Solution.js new file mode 100644 index 0000000000000..bf68ed04ea3ec --- /dev/null +++ b/solution/0200-0299/0219.Contains Duplicate II/Solution.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ +var containsNearbyDuplicate = function(nums, k) { + const d = new Map(); + for (let i = 0; i < nums.length; ++i) { + if (d.has(nums[i]) && i - d.get(nums[i]) <= k) { + return true; + } + d.set(nums[i], i); + } + return false; +}; diff --git a/solution/3100-3199/3163.String Compression III/Solution.js b/solution/3100-3199/3163.String Compression III/Solution.js new file mode 100644 index 0000000000000..9b08e0d39f6fd --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution.js @@ -0,0 +1,22 @@ +/** + * @param {string} word + * @return {string} + */ +var compressedString = function(word) { + const ans = []; + const n = word.length; + for (let i = 0; i < n;) { + let j = i + 1; + while (j < n && word[j] === word[i]) { + ++j; + } + let k = j - i; + while (k) { + const x = Math.min(k, 9); + ans.push(x + word[i]); + k -= x; + } + i = j; + } + return ans.join(''); +};