- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to split string into k distinct partitions
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']
- Related Articles
- Python – Split Numeric String into K digit integers
- Program to find k partitions after truncating sentence using Python
- Check if given string can be split into four distinct strings in Python
- Program to split lists into strictly increasing sublists of size greater than k in Python
- Program to find split a string into the max number of unique substrings in Python
- Program to check a string can be split into three palindromes or not in Python
- Program to check whether we can split a string into descending consecutive values in Python
- Python program to split and join a string?
- Python program to find N-sized substrings with K distinct characters
- Split string into groups - JavaScript
- Python program to split a string and join with comma
- Split string into equal parts JavaScript
- Program to check whether list can be split into sublists of k increasing elements in C++
- PHP program to split a given comma delimited string into an array of values
- Program to find number of distinct combinations that sum up to k in python
