
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Split String into K Distinct Partitions in Python
Suppose we have a string s and and a value k. The value of k is factor of the length of s, say the length is n. We can split s into n/k different substrings called t_i of size k. Then use these t_i to make u_i such that
The characters present in u_i are subsequence of characters in t_i
Any repeat characters will be removed from these string such that frequency of each character in u_i is 1
We have to find these u_i strings
So, if the input is like s = "MMPQMMMRM" k = 3, then the output will be ["MP", "QM", "MR"] because the size of s is 9, and k is 3, so 9/3 = 3. The strings are MMP, QMM and MRM, but as we do not support duplicate characters, then they will be MP, QM and MR.
To solve this, we will follow these steps −
- i := 0
- ret := a new list
- mp := a new map
- to_print := blank string
- while i < size of s, do
- if i mod k is same as 0 and i is not 0, then
- insert to_print at the end of ret
- clear mp and clear to_print
- if s[i] not present in mp, then
- mp[s[i]] := 0
- to_print := to_print + s[i]
- i := i + 1
- if i mod k is same as 0 and i is not 0, then
- insert to_print at the end of ret
- return ret
Example
Let us see the following implementation to get better understanding
def solve(s, k): i = 0 ret = [] mp, to_print = {}, "" while i < len(s): if i % k == 0 and i != 0: ret.append(to_print) mp, to_print = {}, "" if s[i] not in mp.keys(): mp[s[i]] = 0 to_print += s[i] i += 1 ret.append(to_print) return ret s = "MMPQMMMRM" k = 3 print(solve(s, k))
Input
"MMPQMMMRM", 3
Output
['MP', 'QM', 'MR']