Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Aug 10, 2019
2 parents 60e4df1 + 55fc9d7 commit 64d13fb
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode",
"version": "1.3.0",
"version": "1.4.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -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"
}
}
7 changes: 7 additions & 0 deletions problems/is-a-a-majority-element/README.md
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 18 additions & 0 deletions problems/is-a-a-majority-element/solution.js
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions problems/is-a-a-majority-element/solution.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
7 changes: 7 additions & 0 deletions problems/string-transforms-into-another-string/README.md
Original file line number Diff line number Diff line change
@@ -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.
83 changes: 83 additions & 0 deletions problems/string-transforms-into-another-string/solution.js
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions problems/string-transforms-into-another-string/solution.test.js
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 64d13fb

Please sign in to comment.