Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Compare Strings by Frequency of the Smallest Character

Posted on August 25, 2019July 26, 2020 by braindenny

Compare Strings by Frequency of the Smallest Character



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #array, #binarysearch

Let’s define a function f(s) over a non-empty string s, which calculates the frequency of the smallest character in s. For example, if s = “dcce” then f(s) = 2 because the smallest character is “c” and its frequency is 2.

Now, given string arrays queries and words, return an integer array answer, where each answer[i] is the number of words such that f(queries[i]) < f(W), where W is a word in words.

Example 1:

Input: queries = ["cbd"], words = ["zaaaz"]
Output: [1]
Explanation: On the first query we have f("cbd") = 1, f("zaaaz") = 3 so f("cbd") < f("zaaaz").

Example 2:

Input: queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]
Output: [1,2]
Explanation: On the first query only f("bbb") < f("aaaa"). On the second query both f("aaa") and f("aaaa") are both > f("cc").

Constraints:

  • 1 <= queries.length <= 2000
  • 1 <= words.length <= 2000
  • 1 <= queries[i].length, words[i].length <= 10
  • queries[i][j], words[i][j] are English lowercase letters.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/compare-strings-by-frequency-of-the-smallest-character
// Basic Ideas: array + binarysearch
// Complexity: Time O((m+n)*log(m)), Space O(n)
import "sort"
func getFrequency(word string) int {
    chars := make([]int, 26)
    for _, ch := range word {
        chars[ch-'a']++
    }
    for _, v := range chars {
        if v != 0 {
            return v
        }
    }
    return 0
}

func numSmallerByFrequency(queries []string, words []string) []int {
    l := make([]int, len(words))
    for i, word := range words {
        l[i] = getFrequency(word)
    }
    sort.Ints(l)
    res := make([]int, len(queries))
    for i, query := range queries {
        count := getFrequency(query)
        left, right := 0, len(l)-1
        // 3
        // 1 2 3 3 3 4 4 5
        // find the first bigger than the target
        for left <= right {
            mid := (right-left)/2+left
            if l[mid] <= count {
                left = mid+1
            } else {
                right = mid-1
            }
            res[i] = len(l)-left
        }
    }
    return res
}
linkedin
github
slack

Post Views: 0
Posted in BasicTagged #array, binarysearch

Post navigation

LeetCode: Invalid Transactions
LeetCode: Remove Zero Sum Consecutive Nodes from Linked List

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.