Split by k value remove duplicates without changing the order
Problem to split the string by k length and remove the duplicates letters.
Example-1:
Input : n = "AABCAAADA" k = 3 Output : ['AB', 'CA', 'AD'] Explain : step 1: split the string by k length, AAB CAA ADA step 2: Remove duplicates letters AB CA AD
Example-2:
Input : n = "AABBCDDE" k = 2 Output : ['A', 'B', 'CD', 'DE'] Explain : step 1: split the string by k length, AA BB CD DE step 2: Remove duplicates letters A B CD DE
Solution
from collections import OrderedDict import textwrap n = "AABCAAADA" k = 3 l = [] c = textwrap.fill(n,k) e = str(c) d = e.split("\n") for i in range(len(d)): l.append("".join(OrderedDict.fromkeys(d[i]))) print(l)
Output
['AB', 'CA', 'AD']