- 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 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

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))

"momoplaykae"

[4, 1, 1, 4, 1]

- Related Questions & Answers
- Python Program to find the cube of each list element
- Python program to get the indices of each element of one list in another list
- 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
- Program to find sum of the minimums of each sublist from a list in Python
- Find minimum of each index in list of lists in Python
- Python Program to convert a list into matrix with size of each row increasing by a number
- Capitalizing first letter of each word JavaScript
- Python program to get maximum of each key Dictionary List
- Program to find sum of concatenated pairs of all each element in a list in Python?\n
- C++ program to find the Sum of each Row and each Column of a Matrix
- C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome
- Python program to capitalize each word's first letter
- Partition N where the count of parts and each part are a power of 2, and part size and count are restricted in JavaScript
- Program to find size of sublist where product of minimum of A and size of A is maximized in Python

Advertisements