Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create merge_tools.py #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions merge_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def merge_the_tools(string, k):
#divide string into k length substrings
x = 0
y = k
subStrs = []
while (y <= len(string)):
subStrs.append(string[x:y])
x += k
y += k

#remove duplicates in each and print each
for subStr in subStrs:
print remove_dups(subStr)

def remove_dups(string):
newstr = []
for i in range(len(string)):
if string[i] not in string[:i]:
Copy link
Owner Author

@madhuri7112 madhuri7112 Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are iterating through the entire string until ith character for every character so the time complexity of this loop is O(n^2) which is not ideal.

Instead, you can use a set to do this

def remove_dups(string):
   seen_characters = {}
   result = ""
   for i in range(len(string)):
      if  string[i] not in seen_characters:
           result = result + string[i]
      seen_characters.add(string[i])

The time complexity of this loop is only O(n) because it iterates through the string only once and look up in set is O(1) time operation.

newstr.append(string[i])
newstr1 = ""
return newstr1.join(newstr)