The Problem
Consider the following:
-
n
is a length of a strings
. -
k
is a factor ofn
.
We can split s
into
substrings where each subtring,
, consists of a contiguous block of k
characters in s
. Then, use each
to create string
such that:
- The characters in are a subsequence of the characters in .
- Any repeat occurrence of a character is removed from the string such that each character in
occurs exactly once. In other words, if the character at some index
j
in occurs at a previous index< j
in , then do not include the character in string .
Given s
and k
, print
lines where each line i
denotes string
.
Print each subsequence on a new line. There will be of them. No return value is expected.
The Input
- The first line contains a single string,
s
. - The second line contains an integer,
k
, the length of each substring.
Constraints
-
, where
n
is the length ofs
- It is guaranteed that
n
is a multiple ofk
.
Sample:
AABCAAADA
3
The Output
Sample:
AB
CA
AD
Explanation
Split s
into
equal parts of length k = 3
. Convert each
to
by removing any subsequent occurrences of non-distinct characters in
:
Print each on a new line.
The Solution
The Code
Source Code 1
def merge_the_tools(string, k):
l = len(string)//k
for i in range(l):
print(''.join(dict.fromkeys(string[i*k:(i*k)+k])))
Source Code 2
from collections import OrderedDict as od
def merge_the_tools(string, k):
l = len(string)//k
for i in range(l):
print(''.join(od.fromkeys(string[i*k:(i*k)+k])))
Top comments (0)