Last active
August 6, 2017 15:31
-
-
Save clock21am/169ac59c2b0303459ad93509cb091315 to your computer and use it in GitHub Desktop.
Problem Anagram substring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the added function. | |
class Dict { | |
value get(key); | |
put(key, value); | |
boolean contains(key); | |
} | |
def is_anagram(s1, s2): | |
c1 = Dict(); | |
c2 = Dict(); | |
for i in s1: | |
if c1.contains(ord(i)): | |
c1.put(ord(i)) += 1 | |
else: | |
c1.put(ord(i)) = 1 | |
for i in s2: | |
if c2.contains(ord(i)): | |
c2.put(ord(i)) += 1 | |
else: | |
c2.put(ord(i)) = 1 | |
for keys in c1: | |
if c1[keys] != c1[keys]: | |
return False | |
return True | |
def is_subanagram(s1, s2): | |
l = len(s1) | |
start = 0 | |
end = l | |
while(end <= len(s2)): | |
sub = s2[start:end] | |
if is_anagram(s1, sub): | |
return True | |
elif sub[-1] not in s1: | |
start += l | |
end += l | |
else: | |
start += 1 | |
end += 1 | |
return False | |
def question1(a, b): | |
return is_subanagram(b, a) | |
pattern = "Google" | |
match = "Gog" | |
answer = question1(pattern, match) | |
print "anagram substring '{}' in '{}': {}".format(match, pattern, answer) | |
pattern = "Google" | |
match = "Goo" | |
answer = question1(pattern, match) | |
print "anagram substring '{}' in '{}': {}".format(match, pattern, answer) | |
pattern = "Google" | |
match = "Got Answe" | |
answer = question1(pattern, match) | |
print "anagram substring {} in '{}': {}".format(match, pattern, answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment