- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of words and another value k. We have to find the number of sublists in the given words such that there are exactly k different words.

So, if the input is like words = ["Kolkata", "Delhi", "Delhi", "Kolkata"] k = 2, then the output will be 5, as the following sublists have 2 unique words: ["Kolkata", "Delhi"], ["Delhi", "Kolkata"],["Kolkata","Delhi","Delhi"], ["Delhi","Delhi","Kolkata"], ["Kolkata","Delhi","Delhi","Kolkata"], but not ["Delhi","Delhi"] as there is only one unique word.

To solve this, we will follow these steps −

- Define a function work() . This will take words, k
- n := size of words
- if k is same as 0, then
- return 0

- cnt := a new map
- ans := 0
- l := 0
- for r in range 0 to n, do
- word := words[r]
- if word is not present in cnt, then
- cnt[word] := 0

- cnt[word] := cnt[word] + 1
- while size of cnt > k, do
- cnt[words[l]] := cnt[words[l]] - 1
- if cnt[words[l]] is same as 0, then
- remove words[l] from cnt

- l := l + 1

- ans := ans + r - l + 1

- return ans

From the main method return (work(words, k) - work(words, k - 1))

Let us see the following implementation to get better understanding −

class Solution: def solve(self, words, k): return self.work(words, k) - self.work(words, k - 1) def work(self, words, k): n = len(words) if k == 0: return 0 cnt = dict() ans = 0 l = 0 for r in range(n): word = words[r] if word not in cnt: cnt[word] = 0 cnt[word] += 1 while len(cnt) > k: cnt[words[l]] -= 1 if cnt[words[l]] == 0: del cnt[words[l]] l += 1 ans += r - l + 1 return ans ob = Solution() words = ["Kolkata", "Delhi", "Delhi", "Kolkata"] k = 2 print(ob.solve(words, k))

["Kolkata", "Delhi", "Delhi", "Kolkata"], 2

5

- Related Questions & Answers
- Program to count number of sublists with exactly k unique elements in Python
- Program to find max values of sublists of size k in Python
- Program to find number of sublists with sum k in a binary list in Python
- Program to find minimum largest sum of k sublists in C++
- Program to find total number of strings, that contains one unique characters in Python
- Find nth number that contains the digit k or divisible by k in C++
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Program to find number of distinct combinations that sum up to k in python
- Program to find number of sublists whose sum is given target in python
- Program to find number of different subsequences GCDs in Python
- Print all words occurring in a sentence exactly K times
- Find the largest interval that contains exactly one of the given N integers In C++
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find max number of K-sum pairs in Python
- Program to find number of increasing subsequences of size k in Python

Advertisements