-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_1.py
More file actions
57 lines (45 loc) · 1.61 KB
/
question_1.py
File metadata and controls
57 lines (45 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Question 1
# Given two strings s and t, determine whether some anagram of t is
# a substring of s. For example: if s = "udacity" and t = "ad",
# then the function returns True. Your function definition should look like:
# question1(s, t) and return a boolean True or False.
# Find out if s1 and s2 strings are anagram of each other
# @param {string, string} input strings
# @return {bool} if string are anagram of each other or not
def is_anagram(s1, s2):
s1 = list(s1)
s2 = list(s2)
s1.sort()
# Quick sort O(n*log(n))
s2.sort()
return s1 == s2
# Find out sorted(possible substring of s) and compare with sorted(t).
# @params {string, string} input strings
# @return {bool} Question1 answer
def question1(s, t):
if s == None or t == None:
return False
match_length = len(t)
pattern_length = len(s)
for i in range(pattern_length - match_length + 1):
if is_anagram(s[i: i+match_length], t):
return True
return False
# Test cases:
# String S
# String t
# Case 1: question1("", "") -> True
print question1("", "")
# two empty strings have the same values
# Case 2: question1("Hello", "llo" -> True
print question1("Hello", "lol")
# string with 3 letters not same as in order as S
# Case 3: question1("Hello", "le") -> True
print question1("Hello", "le")
# string with 2 letters with new letter not in string S
# Case 4: question1("Hello", "Helloo") -> False
print question1("Hello", "Helloobr")
# substring t's length is greater than string S
# Case 5: question1("Hello", "Hello") -> True
print question1("Hello", None)
# String t has the same value as String S