
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find number of sublists that contains exactly k different words in Python
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))
Example (Python)
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))
Input
["Kolkata", "Delhi", "Delhi", "Kolkata"], 2
Input
5
- Related Articles
- Program to count number of sublists with exactly k unique elements in Python
- Program to find number of sublists with sum k in a binary list in Python
- Program to find max values of sublists of size k in Python
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Program to find minimum largest sum of k sublists in C++
- Program to find number of sublists whose sum is given target in python
- Program to find total number of strings, that contains one unique characters in Python
- Program to find number of distinct combinations that sum up to k in python
- Program to find k sublists with largest sums and return sums in ascending order in Python
- Program to maximize the minimum value after increasing K sublists in Python
- Find nth number that contains the digit k or divisible by k in C++
- Python program to remove K length words in String
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find number of different subsequences GCDs in Python
- Program to split lists into strictly increasing sublists of size greater than k in Python

Advertisements