Menu Close

Partition the string into as many parts as possible.

A string S of lowercase English letters is given.
We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.

Input: S = “ababcbacadefegdehijhklij”
Output: [9,7,8]

Explanation:
The partition is “ababcbaca”, “defegde”, “hijhklij”.
This is a partition so that each letter appears in at most one part.
A partition like “ababcbacadefegde”, “hijhklij” is incorrect, because it splits S into less parts.

String=input("Enter the String: ")
i=0
N=len(String)
list1=[]
while i<N:
	index=String.rindex(String[i])
	j=i
	while (j<index):
		if(String.rindex(String[j])>index and j!=i):
			index=String.rindex(String[j])
		j+=1
	list1.append(String[i:index+1])
	i=index+1
length=[]
for i in list1:
	length.append(len(i))
print(length)

Input:
Enter the String: ababcbacadefegdehijhklij

Output:
             [9, 7, 8]


exec. on terminal


More Codes to Fcuk