Skip to content

Commit d00c09d

Browse files
author
Partho Biswas
committed
1170_Compare_Strings_by_Frequency_of_the_Smallest_Character
1 parent 2c613d3 commit d00c09d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ I have solved quite a number of problems from several topics. See the below tabl
300300
|21| [702. Search in a Sorted Array of Unknown Size](https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/)| [Python](https://tinyurl.com/wu6rdaw/702_Search_in_a_Sorted_Array_of_Unknown_Size.py)| [Educative.io](https://tinyurl.com/qn6uuel) | Medium | 📌 [Binary Search Template I](https://leetcode.com/explore/learn/card/binary-search/125/template-i/938/) |
301301
|22| **[378. Kth Smallest Element in a Sorted Matrix](https://tinyurl.com/shz4289)** | [Python](https://tinyurl.com/wu6rdaw/378_Kth_Smallest_Element_in_a_Sorted_Matrix.py)| [educative.io](https://tinyurl.com/scbjgjd) | Hard | 📌 TODO: Check again the Binary Search approach. Very important |
302302
|23| **[1146. Snapshot Array](https://tinyurl.com/v3nb29v)** | [Python](https://tinyurl.com/wu6rdaw/1146_Snapshot_Array.py)| [Art 1](https://tinyurl.com/v3nb29v) | Medium | Tricky use of binary search |
303+
|24| **[1170. Compare Strings by Frequency of the Smallest Character](https://tinyurl.com/tcr34w3)** | [Python](https://tinyurl.com/wu6rdaw/1170_Compare_Strings_by_Frequency_of_the_Smallest_Character.py)| --- | Easy | Binary Search, Very important |
303304

304305
</p>
305306
</details>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import bisect
2+
from collections import Counter
3+
class Solution(object):
4+
def numSmallerByFrequency(self, queries, words):
5+
"""
6+
:type queries: List[str]
7+
:type words: List[str]
8+
:rtype: List[int]
9+
"""
10+
wordFrequecBySmallest = []
11+
for word in words:
12+
wordFrequecBySmallest.append(self.getFrequecBySmallest(word))
13+
wordFrequecBySmallest.sort()
14+
answer = []
15+
for querie in queries:
16+
smallentInQuery = self.getFrequecBySmallest(querie)
17+
insertionPoint = bisect.bisect_left(wordFrequecBySmallest, smallentInQuery)
18+
wordCount = len(wordFrequecBySmallest) - insertionPoint
19+
answer.append(wordCount)
20+
return answer
21+
22+
23+
def getFrequecBySmallest(self, word):
24+
if not word:
25+
return 0
26+
counter = Counter(word)
27+
keys = sorted(counter.keys())
28+
return counter[keys[0]]
29+
30+

0 commit comments

Comments
 (0)