Skip to content

Commit

Permalink
Merge branch 'release/1.10.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Aug 25, 2019
2 parents 22f0faa + 521a17b commit 609c40c
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Folder structure in this repository: https://<span></span>github.com/ecgan/leetc
| 1163 | [Last Substring in Lexicographical Order](/problems/last-substring-in-lexicographical-order) | Hard | String, suffix array |
| 1165 | [Single-Row Keyboard](/problems/single-row-keyboard) | Easy | String |
| 1167 | [Minimum Cost to Connect Sticks](/problems/minimum-cost-to-connect-sticks) | Medium | Greedy |
| 1170 | [Compare Strings by Frequency of the Smallest Character](/problems/compare-strings-by-frequency-of-the-smallest-character) | Easy | Array, string |

## Questions / Issues

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode",
"version": "1.9.0",
"version": "1.10.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand Down
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.
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
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])
})

0 comments on commit 609c40c

Please sign in to comment.