|
| 1 | +""" |
| 2 | +Python version 3.7.0 |
| 3 | +1.9 - String Rotation |
| 4 | +Assume you have a method isSubstring which |
| 5 | +checks if one word is a substring |
| 6 | +of another. Given two strings, s1 and s2, |
| 7 | +write code to check if s2 is a |
| 8 | +rotation of s1 using only one call to isSubstring |
| 9 | +(e.g. 'waterbottle' is a rotation of 'erbottlewat') |
| 10 | +""" |
| 11 | +import unittest |
| 12 | + |
| 13 | + |
| 14 | +def is_substring(sub: str, s: str) -> bool: |
| 15 | + """ |
| 16 | + is_substring checks if 'sub' is a substring of 's' |
| 17 | + :param sub: the substring |
| 18 | + :param s: the alleged full string |
| 19 | + :return: true if sub is a substring of s, false otherwise |
| 20 | + """ |
| 21 | + return sub in s |
| 22 | + |
| 23 | + |
| 24 | +def string_rotation(s1: str, s2: str) -> bool: |
| 25 | + """ |
| 26 | + Given two strings, string_rotation will check if s2 is a rotation of s1 |
| 27 | + using only one call to isSubstring. |
| 28 | + Assume s2 is a rotation of s1. s1 is a rearrangement of s2. |
| 29 | + Choosing an index where we rotate to be any index, |
| 30 | + let s1 and s2 consist of two parts, a and b. |
| 31 | + The original string will be s2 = ab. |
| 32 | + The rotated string will be s1 = ba. |
| 33 | + Since we are using substring, we want s2 = ab to appear in |
| 34 | + a larger string ..ab.. |
| 35 | + This is guaranteed to happen if we have s1 + s1 = baba |
| 36 | + because ab shows up in there. |
| 37 | + Runtime: worst case: O(n^2) - substring found at end of full string |
| 38 | + Space Complexity: O(n) |
| 39 | + :param s1: the rotated string |
| 40 | + :param s2: the original string |
| 41 | + :return: true if s2 is a rotation of s1, false otherwise |
| 42 | + """ |
| 43 | + # not rotation if lengths don't match |
| 44 | + if len(s1) != len(s2): |
| 45 | + return False |
| 46 | + return is_substring(s2, s1 + s1) |
| 47 | + |
| 48 | + |
| 49 | +class TestStringRotation(unittest.TestCase): |
| 50 | + |
| 51 | + def setUp(self): |
| 52 | + self.str_rotation_cases = [ |
| 53 | + ('erbottlewat', 'waterbottle', True), |
| 54 | + ('erbottlewet', 'weterbottle', True), |
| 55 | + ('same', 'same', True), |
| 56 | + ('oneoff', 'oneof', False), |
| 57 | + ('erbottleabc', 'waterbottle', False), |
| 58 | + ('ion?rotat', 'rotation?', True), |
| 59 | + ('', '', True), |
| 60 | + ('a', 'a', True), |
| 61 | + ('a', 'b', False), |
| 62 | + ('erbottlewat', 'eeeeeeeeee', False) |
| 63 | + ] |
| 64 | + |
| 65 | + self.is_substring_cases = [ |
| 66 | + ('swag', 'swagger', True), |
| 67 | + ('swagger', 'swag', False), |
| 68 | + ('test', 'testing', True), |
| 69 | + ('', 'sdfkjslf', True), |
| 70 | + ('abcd', 'defg', False) |
| 71 | + ] |
| 72 | + |
| 73 | + def test_is_substring(self): |
| 74 | + for sub, s, expected in self.is_substring_cases: |
| 75 | + self.assertEqual(is_substring(sub, s), expected, msg=(sub, s, expected)) |
| 76 | + |
| 77 | + def test_string_rotation(self): |
| 78 | + for s1, s2, expected in self.str_rotation_cases: |
| 79 | + self.assertEqual(string_rotation(s1, s2), expected, msg=(s1, s2, expected)) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + unittest.main() |
0 commit comments