Consider the following:
- A string, , of length where .
- An integer, , where is a factor of .
We can split into subsegments where each subsegment, , consists of a contiguous block of characters
in . Then, use each to create string such that:
in . 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 in occurs at a previous index in , then do not include the character in string .
Given and , print lines where each line denotes string .
Input Format
The first line contains a single string denoting .
The second line contains an integer, , denoting the length of each subsegment.
The second line contains an integer, , denoting the length of each subsegment.
Constraints
- , where is the length of
- It is guaranteed that is a multiple of .
Output Format
Print lines where each line contains string .
Sample Input
AABCAAADA
3
Sample Output
AB
CA
AD
Explanation
String is split into equal parts of length . We convert each to by removing any subsequent occurrences non-distinct characters in :
We then print each on a new line.
Merge the Tools! - Hacker Rank Solution
The basic algorithm for solving this challenge is as follows:
- Divide string into subsegments of length .
- Save the subsegment as variable . For each :
- Create a variable, , and initialize it to the empty string.
- Iterate over the characters in and append each character to that does not already exist in .
- After iterating through all the characters in , print the value of .
Merge the Tools! - Hacker Rank Solution
Python 2
s = raw_input()
k = input()
chunks = len(s)/k
for index in xrange(chunks):
merge = ""
T = s[index*k : (index+1)*k]
for ch in T:
if ch not in merge:
merge += ch
print merge
Python 3
s = input()
k = int(input())
num_subsegments = int(len(s) / k)
for index in range(num_subsegments):
# Subsegment string
t = s[index * k : (index + 1) * k]
# Subsequence string having distinct characters
u = ""
# If a character is not already in 'u', append
for c in t:
if c not in u:
u += c
# Print final converted string
print(u)
abc=''
ReplyDelete# cutting=len(string)//k
for i in range(len(string)):
if string[i] not in abc:
abc+=string[i]
if ((i+1)%k)==0:
if abc!='':
print(abc)
abc=''
def merge_the_tools(string, k):
ReplyDelete# your code goes here
n = int((len(string))/k)
l = [ ]
len_l = 0
for i in string:
len_l = len_l + 1
if i not in l:
l.append(i)
if len_l == k:
print (''.join(l))
len_l = 0
l = [ ]
if __name__ == '__main__':
string, k = input(), int(input())
merge_the_tools(string, k)
NO IDENTATION
Deleteso error
def merge_the_tools(string, k):
ReplyDeletebrk = [string[i:i+k] for i in range(0,len(string),k)]
for j in brk:
dd = list(j)
s = ""
for i in dd:
if i in s:
continue
else:
s +=i
print(s)
def merge_the_tools(string, k):
ReplyDeletepos=[]
for i in range(0,len(string),k):
pos.append(i)
l=[]
for j in pos:
x=string[j:j+k]
p=[]
for j in x:
if j not in p:
p.append(j)
else:
continue
s=''
p=s.join(p)
l.append(p)
print(*l,sep='\n')
if __name__ == '__main__':
string, k = input(), int(input())
merge_the_tools(string, k)
def merge_the_tools(string, k):
ReplyDeletel=int(len(string)/k)
t=[]
m=0
s=0
str=" "
newL=[]
for i in range(int(l)):
m=m+l
t.append(string[s:m])
s=s+l
for i in t:
i.split()
for j in i:
if j not in str:
str=str+j
newL.append(str)
str=" "
for i in newL:
print(i)
# your code goes here
if __name__ == '__main__':
string, k = input(), int(input())
merge_the_tools(string, k)
it working well but test cases not
Deletedef merge_the_tools(s,k):
ReplyDeletesep=len(s)/k
ans=""
for i in range(len(s)):
if (i+1)%k==0:
ans+=s[i]
a=""
for j in ans:
if j not in a:
a+=j
print(a)
ans=""
else:
ans+=s[i]
s="AABCAAADA"
ReplyDeletek=3
length= len(s)
letters=[]
for i in range(length):
letters.append(s[i])
print(letters)
substrings=[]
for i in range(length//k):
word=""
j=k
array=[]
while(j>0):
array.append(letters.pop(0))
j-=1
word=word.join(array)
u=""
for w in word:
if w not in u:
u+=w
print(u)
def merge_the_tools(string, k):
ReplyDeletea=[]
l=len(string)+1
for i in range(1,l):
if i%k==0:
if i==k:
a.append(string[-i:])
else:
j=-i+3
a.append(string[-i:j])
a.reverse()
for i in range(0,len(a)):
a[i]=list(a[i])
a[i]=list(dict.fromkeys(a[i]))
a[i]="".join(a[i])
print(a[i])
if __name__ == '__main__':
string, k = raw_input(), int(raw_input())
merge_the_tools(string,
def merge_the_tools(string, k):
ReplyDeletenum_subsegments = int(len(string) / k)
for index in range(0,len(string),k):
sub_string = string[index: index + k]
#print(sub_string)
empty = ""
for i in sub_string:
if i not in empty:
empty += i
print(empty)
if __name__ == '__main__':
string, k = input(), int(input())
merge_the_tools(string, k)