From dddfc594d0a5bc4ec956639e02ee0537a35cfa0f Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 11 Aug 2019 01:26:35 +0800 Subject: [PATCH 1/5] added lodash. --- package-lock.json | 5 ++--- package.json | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 917d85a..06600a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "leetcode", - "version": "1.0.0", + "version": "1.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3845,8 +3845,7 @@ "lodash": { "version": "4.17.15", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", - "dev": true + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" }, "lodash.sortby": { "version": "4.7.0", diff --git a/package.json b/package.json index 376138e..7c326b7 100644 --- a/package.json +++ b/package.json @@ -28,5 +28,8 @@ "eslint-plugin-promise": "^4.2.1", "eslint-plugin-standard": "^4.0.0", "jest": "^24.8.0" + }, + "dependencies": { + "lodash": "^4.17.15" } } From 9d13c25012819512ea6a08b50480fc6e50bb5a3f Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 11 Aug 2019 01:33:31 +0800 Subject: [PATCH 2/5] added solution for "Check If a Number Is Majority Element in a Sorted Array". --- problems/is-a-a-majority-element/README.md | 7 +++++++ problems/is-a-a-majority-element/solution.js | 18 ++++++++++++++++++ .../is-a-a-majority-element/solution.test.js | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 problems/is-a-a-majority-element/README.md create mode 100644 problems/is-a-a-majority-element/solution.js create mode 100644 problems/is-a-a-majority-element/solution.test.js diff --git a/problems/is-a-a-majority-element/README.md b/problems/is-a-a-majority-element/README.md new file mode 100644 index 0000000..631e6ac --- /dev/null +++ b/problems/is-a-a-majority-element/README.md @@ -0,0 +1,7 @@ +# Check If a Number Is Majority Element in a Sorted Array + +[Link to LeetCode page](https://leetcode.com/problems/is-a-a-majority-element/) + +Difficulty: Easy + +Topics: Array, binary search. diff --git a/problems/is-a-a-majority-element/solution.js b/problems/is-a-a-majority-element/solution.js new file mode 100644 index 0000000..65a800a --- /dev/null +++ b/problems/is-a-a-majority-element/solution.js @@ -0,0 +1,18 @@ +const isMajorityElement = (nums, target) => { + const minimum = nums.length / 2 + let count = 0 + + for (const value of nums) { + if (value === target) { + count += 1 + } + + if (value > target) { + break + } + } + + return (count > minimum) +} + +module.exports = isMajorityElement diff --git a/problems/is-a-a-majority-element/solution.test.js b/problems/is-a-a-majority-element/solution.test.js new file mode 100644 index 0000000..930ebb9 --- /dev/null +++ b/problems/is-a-a-majority-element/solution.test.js @@ -0,0 +1,19 @@ +const isMajorityElement = require('./solution') + +test('Example 1', () => { + const nums = [2, 4, 5, 5, 5, 5, 5, 6, 6] + const target = 5 + + const result = isMajorityElement(nums, target) + + expect(result).toBe(true) +}) + +test('Example 2', () => { + const nums = [10, 100, 101, 101] + const target = 101 + + const result = isMajorityElement(nums, target) + + expect(result).toBe(false) +}) From 10999bb605d4cfd73aa1a7914f58ca3caf333241 Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 11 Aug 2019 01:35:27 +0800 Subject: [PATCH 3/5] added solution for "String Transforms Into Another String". --- .../README.md | 7 ++ .../solution.js | 83 +++++++++++++++++++ .../solution.test.js | 55 ++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 problems/string-transforms-into-another-string/README.md create mode 100644 problems/string-transforms-into-another-string/solution.js create mode 100644 problems/string-transforms-into-another-string/solution.test.js diff --git a/problems/string-transforms-into-another-string/README.md b/problems/string-transforms-into-another-string/README.md new file mode 100644 index 0000000..9ea53d9 --- /dev/null +++ b/problems/string-transforms-into-another-string/README.md @@ -0,0 +1,7 @@ +# String Transforms Into Another String + +[Link to LeetCode page](https://leetcode.com/problems/string-transforms-into-another-string/) + +Difficulty: Hard + +Topics: Graph. diff --git a/problems/string-transforms-into-another-string/solution.js b/problems/string-transforms-into-another-string/solution.js new file mode 100644 index 0000000..90b67a1 --- /dev/null +++ b/problems/string-transforms-into-another-string/solution.js @@ -0,0 +1,83 @@ +const isEqual = require('lodash/isEqual') +const difference = require('lodash/difference') + +const canConvert = (str1, str2) => { + if (str1 === str2) { + return true + } + + const chars2 = [...str2] + const charMap2 = {} + const segment2 = [0] + for (let i = 0; i < chars2.length; i++) { + const char = chars2[i] + + if (charMap2[char]) { + charMap2[char].push(i) + } else { + charMap2[char] = [i] + } + + if (i > 0 && (chars2[i] !== chars2[i - 1])) { + segment2.push(i) + } + } + + const chars1 = [...str1] + const charMap1 = {} + const segment1 = [0] + for (let i = 0; i < chars1.length; i++) { + const char = chars1[i] + + if (charMap1[char]) { + charMap1[char].push(i) + } else { + charMap1[char] = [i] + } + + if (i > 0 && (chars1[i] !== chars1[i - 1])) { + segment1.push(i) + } + } + + const usedChars1 = Object.keys(charMap1).length + const usedChars2 = Object.keys(charMap2).length + + if (usedChars1 === 26 && usedChars2 === 26) { + return false + } + + if (usedChars1 < usedChars2) { + return false + } + + if (segment1.length < segment2.length) { + return false + } + + if (isEqual(segment1, segment2)) { + return true + } + + for (const char2 in charMap2) { + const indexes = charMap2[char2] + + for (const i of indexes) { + const char1 = chars1[i] + const char1indexes = charMap1[char1] + + if (char1indexes.length > indexes.length) { + return false + } + + const diff = difference(char1indexes, indexes) + if (diff.length > 0) { + return false + } + } + } + + return true +} + +module.exports = canConvert diff --git a/problems/string-transforms-into-another-string/solution.test.js b/problems/string-transforms-into-another-string/solution.test.js new file mode 100644 index 0000000..3615894 --- /dev/null +++ b/problems/string-transforms-into-another-string/solution.test.js @@ -0,0 +1,55 @@ +const canConvert = require('./solution') + +test('Example 1', () => { + const str1 = 'aabcc' + const str2 = 'ccdee' + + const result = canConvert(str1, str2) + + expect(result).toBe(true) +}) + +test('Example 2', () => { + const str1 = 'leetcode' + const str2 = 'codeleet' + + const result = canConvert(str1, str2) + + expect(result).toBe(false) +}) + +test('"ab" and "ba" should return true', () => { + const str1 = 'ab' + const str2 = 'ba' + + const result = canConvert(str1, str2) + + expect(result).toBe(true) +}) + +test('"abcdefghijklmnopqrstuvwxyz" and "abcdefghijklmnopqrstuvwxyz" should return true', () => { + const str1 = 'abcdefghijklmnopqrstuvwxyz' + const str2 = 'abcdefghijklmnopqrstuvwxyz' + + const result = canConvert(str1, str2) + + expect(result).toBe(true) +}) + +test('"abcdefghijklmnopqrstuvwxyz" and "bcdefghijklmnopqrstuvwxyza" should return false', () => { + const str1 = 'abcdefghijklmnopqrstuvwxyz' + const str2 = 'bcdefghijklmnopqrstuvwxyza' + + const result = canConvert(str1, str2) + + expect(result).toBe(false) +}) + +test('"abcdefghijklmnopqrstuvwxyz" and "bcdefghijklmnopqrstuvwxyzq" should return true', () => { + const str1 = 'abcdefghijklmnopqrstuvwxyz' + const str2 = 'bcdefghijklmnopqrstuvwxyzq' + + const result = canConvert(str1, str2) + + expect(result).toBe(true) +}) From 6acf05facd76672b013e38d05ed646dbbd1acff6 Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 11 Aug 2019 01:41:12 +0800 Subject: [PATCH 4/5] added new solution rows in the main readme. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 941c38e..da6775f 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ npm run test | 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation | | 349 | [Intersection of Two Arrays](/problems/intersection-of-two-arrays) | Easy | Hash Table, two pointers, binary search, sort, set | | 1144 | [Decrease Elements To Make Array Zigzag](/problems/decrease-elements-to-make-array-zigzag) | Medium | Array | +| 1150 | [Check If a Number Is Majority Element in a Sorted Array](/problems/is-a-a-majority-element) | Easy | Array, binary search | +| 1153 | [String Transforms Into Another String](/problems/string-transforms-into-another-string) | Hard | Graph | ## Questions / Issues From 55fc9d702103e43416f2855aa8d59729a501745b Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 11 Aug 2019 01:46:33 +0800 Subject: [PATCH 5/5] version bump to 1.4.0. --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 06600a2..1b6d1d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "leetcode", - "version": "1.3.0", + "version": "1.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7c326b7..5f5877d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "leetcode", - "version": "1.3.0", + "version": "1.4.0", "description": "My solutions for LeetCode problems.", "main": "index.js", "scripts": {