
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find size of each partition of a list where each letter appears at most one piece in Python
Suppose we have a lowercase string s, we can partition s into as many pieces as possible such that each letter appears in at most one piece and find the sizes of the partitions as a list.
So, if the input is like s = "momoplaykae", then the output will be [4, 1, 1, 4, 1], as the string is split into ["momo", "p", "l", "ayka", "e"].
To solve this, we will follow these steps −
count := a map that contains characters in s and their occurrences
out := a new list, stk := an empty stack
length := 0
for each char in s, do
count[char] := count[char] - 1
length := length + 1
while count[char] is not same as 0 or stk is not empty, do
if count[char] is not same as 0, then
push char into stk
come out from loop
if stk is not empty and count[top of stk] is same as 0, then
pop from stk
otherwise,
come out from the loop
if stk is empty and count[char] is same as 0, then
insert length after out
length := 0
return out
Example
Let us see the following implementation to get a better understanding −
from collections import Counter class Solution: def solve(self, s): count = Counter(s) out = [] stk = [] length = 0 for char in s: count[char] -= 1 length += 1 while count[char] != 0 or stk: if count[char] != 0: stk.append(char) break if stk and count[stk[-1]] == 0: stk.pop() else: break if not stk and count[char] == 0: out += [length] length = 0 return out ob = Solution() s = "momoplaykae" print(ob.solve(s))
Input
"momoplaykae"
Output
[4, 1, 1, 4, 1]
- Related Articles
- Program to find a list of numbers where each K-sized window has unique elements in Python
- Program to partition two strings such that each partition forms anagram in Python
- Python program to get the indices of each element of one list in another list
- Python Program to find the cube of each list element
- Program to find sum of the minimums of each sublist from a list in Python
- Python Program to convert a list into matrix with size of each row increasing by a number
- Program to partition color list in Python
- Python program to capitalize each word's first letter
- Program to find sum of concatenated pairs of all each element in a list in Python?\n
- Partition N where the count of parts and each part are a power of 2, and part size and count are restricted in JavaScript
- C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome
- Program to find size of sublist where product of minimum of A and size of A is maximized in Python
- Python program to get maximum of each key Dictionary List
- Python program to find the group sum till each K in a list
- Program to find a sub-list of size at least 2 whose sum is multiple of k in Python
