-
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
6 changed files
with
92 additions
and
2 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
7 changes: 7 additions & 0 deletions
7
problems/compare-strings-by-frequency-of-the-smallest-character/README.md
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 @@ | ||
# Compare Strings by Frequency of the Smallest Character | ||
|
||
LeetCode #: [1170](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/) | ||
|
||
Difficulty: Easy | ||
|
||
Topics: Array, string. |
45 changes: 45 additions & 0 deletions
45
problems/compare-strings-by-frequency-of-the-smallest-character/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,45 @@ | ||
const sortBy = require('lodash/sortBy') | ||
const sortedLastIndex = require('lodash/sortedLastIndex') | ||
|
||
const f = (s) => { | ||
let character = s[0] | ||
let frequency = 1 | ||
|
||
for (let i = 1; i < s.length; i++) { | ||
const c = s[i] | ||
|
||
if (c === character) { | ||
frequency += 1 | ||
} | ||
|
||
if (c < character) { | ||
character = c | ||
frequency = 1 | ||
} | ||
} | ||
|
||
return frequency | ||
} | ||
|
||
const getResult = (queriesFreqs, sortedWordsFreqs) => { | ||
const result = [] | ||
for (let i = 0; i < queriesFreqs.length; i++) { | ||
const qf = queriesFreqs[i] | ||
const insertIndex = sortedLastIndex(sortedWordsFreqs, qf) | ||
const number = sortedWordsFreqs.length - insertIndex | ||
|
||
result.push(number) | ||
} | ||
|
||
return result | ||
} | ||
|
||
const numSmallerByFrequency = (queries, words) => { | ||
const wordsFreqs = words.map(w => f(w)) | ||
const sortedWordsFreqs = sortBy(wordsFreqs) | ||
const queriesFreqs = queries.map(q => f(q)) | ||
|
||
return getResult(queriesFreqs, sortedWordsFreqs) | ||
} | ||
|
||
module.exports = numSmallerByFrequency |
37 changes: 37 additions & 0 deletions
37
problems/compare-strings-by-frequency-of-the-smallest-character/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,37 @@ | ||
const numSmallerByFrequency = require('./solution') | ||
|
||
test('Example 1', () => { | ||
const queries = ['cbd'] | ||
const words = ['zaaaz'] | ||
|
||
const result = numSmallerByFrequency(queries, words) | ||
|
||
expect(result).toStrictEqual([1]) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const queries = ['bbb', 'cc'] | ||
const words = ['a', 'aa', 'aaa', 'aaaa'] | ||
|
||
const result = numSmallerByFrequency(queries, words) | ||
|
||
expect(result).toStrictEqual([1, 2]) | ||
}) | ||
|
||
test('Contain very long query', () => { | ||
const queries = ['bbb', 'cc', 'ddddddddd'] | ||
const words = ['a', 'aa', 'aaa', 'aaaa'] | ||
|
||
const result = numSmallerByFrequency(queries, words) | ||
|
||
expect(result).toStrictEqual([1, 2, 0]) | ||
}) | ||
|
||
test('long arrays', () => { | ||
const queries = ['bba', 'abaaaaaa', 'aaaaaa', 'bbabbabaab', 'aba', 'aa', 'baab', 'bbbbbb', 'aab', 'bbabbaabb'] | ||
const words = ['aaabbb', 'aab', 'babbab', 'babbbb', 'b', 'bbbbbbbbab', 'a', 'bbbbbbbbbb', 'baaabbaab', 'aa'] | ||
|
||
const result = numSmallerByFrequency(queries, words) | ||
|
||
expect(result).toStrictEqual([6, 1, 1, 2, 3, 3, 3, 1, 3, 2]) | ||
}) |