From ba95d8c6459f3f7583bf3054c87b1415a2bb09fa Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 25 Aug 2019 22:52:50 +0800 Subject: [PATCH 1/3] added solution for #1170 Compare Strings by Frequency of the Smallest Character. --- .../README.md | 7 +++ .../solution.js | 45 +++++++++++++++++++ .../solution.test.js | 37 +++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 problems/compare-strings-by-frequency-of-the-smallest-character/README.md create mode 100644 problems/compare-strings-by-frequency-of-the-smallest-character/solution.js create mode 100644 problems/compare-strings-by-frequency-of-the-smallest-character/solution.test.js diff --git a/problems/compare-strings-by-frequency-of-the-smallest-character/README.md b/problems/compare-strings-by-frequency-of-the-smallest-character/README.md new file mode 100644 index 0000000..00d6bb7 --- /dev/null +++ b/problems/compare-strings-by-frequency-of-the-smallest-character/README.md @@ -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. diff --git a/problems/compare-strings-by-frequency-of-the-smallest-character/solution.js b/problems/compare-strings-by-frequency-of-the-smallest-character/solution.js new file mode 100644 index 0000000..319e302 --- /dev/null +++ b/problems/compare-strings-by-frequency-of-the-smallest-character/solution.js @@ -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 diff --git a/problems/compare-strings-by-frequency-of-the-smallest-character/solution.test.js b/problems/compare-strings-by-frequency-of-the-smallest-character/solution.test.js new file mode 100644 index 0000000..effb471 --- /dev/null +++ b/problems/compare-strings-by-frequency-of-the-smallest-character/solution.test.js @@ -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]) +}) From 0861134696b103e195e3dea54d7971d7d6006038 Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 25 Aug 2019 22:54:27 +0800 Subject: [PATCH 2/3] added row for #1170. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cf28159..7b527d4 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ Folder structure in this repository: https://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 From 521a17b810ed5467922a3e27fcc4985950b2e580 Mon Sep 17 00:00:00 2001 From: Gan Eng Chin Date: Sun, 25 Aug 2019 22:58:34 +0800 Subject: [PATCH 3/3] version bump to 1.10.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 37060aa..8e67ef1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "leetcode", - "version": "1.9.0", + "version": "1.10.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7280dfd..4de4004 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "leetcode", - "version": "1.9.0", + "version": "1.10.0", "description": "My solutions for LeetCode problems.", "main": "index.js", "scripts": {