Guest User

Untitled

a guest
Jan 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. """Given an input string (s) and a pattern (p), implement wildcard pattern matching with
  2. support for '?' and '*'.
  3.  
  4. '?' Matches any single character.
  5. '*' Matches any sequence of characters (including the empty sequence).
  6. The matching should cover the entire input string (not partial).
  7.  
  8. Note:
  9. s could be empty and contains only lowercase letters a-z.
  10. p could be empty and contains only lowercase letters a-z, and characters like ? or *.
  11.  
  12. Example 1:
  13. Input:
  14. s = "aa"
  15. p = "a"
  16. Output: false
  17. Explanation: "a" does not match the entire string "aa".
  18.  
  19. Example 2:
  20. Input:
  21. s = "aa"
  22. p = "*"
  23. Output: true
  24. Explanation: '*' matches any sequence.
  25.  
  26. Example 3:
  27. Input:
  28. s = "cb"
  29. p = "?a"
  30. Output: false
  31. Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
  32.  
  33. Example 4:
  34. Input:
  35. s = "adceb"
  36. p = "*a*b"
  37. Output: true
  38. Explanation: The first '*' matches the empty sequence, while the second '*'
  39. matches the substring "dce".
  40.  
  41. Example 5:
  42. Input:
  43. s = "acdcb"
  44. p = "a*c?b"
  45. Output: false
  46. """
  47.  
  48.  
  49. class Solution:
  50. def isMatch(self, s, p):
  51. """
  52. :type s: str
  53. :type p: str
  54. :rtype: bool
  55. """
  56. import re
  57. from itertools import groupby
  58.  
  59. grps = groupby(p)
  60. l = [(x, len(list(y))) for x, y in grps]
  61.  
  62. new_p = ''
  63. for c, cnt in l:
  64. if c == '*':
  65. new_p = ''.join([new_p, '[sa-z]*?'])
  66. elif c == '?':
  67. new_p = ''.join([new_p, '.{', str(cnt), '}'])
  68. else:
  69. new_p = ''.join([new_p, c, '{', str(cnt), '}'])
  70.  
  71. print(" new pat: ", p)
  72. MY_RE = re.compile(p, re.A)
  73. return True if MY_RE.fullmatch(s) else False
  74.  
  75.  
  76. # string, patter, expected
  77. inps = [
  78. ("aa", "a", False),
  79. ("aa", "*", True),
  80. ("cb", "?a", False),
  81. ("adceb", "*a*b", True),
  82. ("acdcb", "a*c?b", False),
  83. ("abcdef", "", False),
  84. ("abcdef", "*", True),
  85. ("abcdef", "??????", True),
  86. ("abcdef", "a?c?e?", True),
  87. ("abcdef", "a?c?e?", True),
  88. ("abcdef", "********************", True),
  89. ("abcdef", "*****abcdef*****", True),
  90. ("abcdef", "?abcdef?", False),
  91. ("", "", True),
  92. ("", "*", True),
  93. ("", "*****", True),
  94. ("", "?", False),
  95. ("a", "*********?********", True),
  96. ("a", "?", True),
  97. ("abc", "?*?", True),
  98. (
  99. "aaaabaaaabbbbaabbbaabbaababbabbaaaababaaabbbbbbaabbbabababbaaabaabaaaaaabbaabbbbaababbababaabbbaababbbba",
  100. "*****b*aba***babaa*bbaba***a*aaba*b*aa**a*b**ba***a*a*",
  101. True,
  102. ),
  103. (
  104. "abbabaaabbabbaababbabbbbbabbbabbbabaaaaababababbbabababaabbababaabbbbbbaaaabababbbaabbbbaabbbbababababbaabbaababaabbbababababbbbaaabbbbbabaaaabbababbbbaababaabbababbbbbababbbabaaaaaaaabbbbbaabaaababaaaabb",
  105. "**aa*****ba*a*bb**aa*ab****a*aaaaaa***a*aaaa**bbabb*b*b**aaaaaaaaa*a********ba*bbb***a*ba*bb*bb**a*b*bb",
  106. False,
  107. ),
  108. ]
  109.  
  110. sol = Solution()
  111. for s, p, exp in inps:
  112. print(f"Doing string[{s}] with pattern[{p}] and asserting...")
  113. assert sol.isMatch(s, p) == exp
Add Comment
Please, Sign In to add comment