HackerRank 'Merge The Tools' Solution

Martin Kysel · July 30, 2020

Short Problem Definition:

Split the string S into chunks T. Remove duplicates from T.

Merge The Tools

Complexity:

time complexity is O(N)

space complexity is O(N)

Execution:

First, split the string into chunks. In Python 2, use an ordered dictionary (that preserves insertion order) to discard duplicates. An ordered set would work too. The basic Sets/Maps are already ordered in Python 3.

Solution:
from collections import OrderedDict

def merge_the_tools(string, k):
    chunks = [string[i:i+k] for i in range(0, len(string), k)]
    for chunk in chunks:
        print "".join(OrderedDict.fromkeys(chunk))

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.