-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
83
problems/string-transforms-into-another-string/solution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
problems/string-transforms-into-another-string/solution.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |