1.题目描述
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.
1 | '.' Matches any single character. |
The matching should cover the entire input string (not partial).
给你一个字符串s和一个字符规律p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。
‘.’ 匹配任意单个字符
‘*’ 匹配零个或多个前面的那一个元素
所谓匹配,是要涵盖整个字符串s的,而不是部分字符串。
Note:
scould be empty and contains only lowercase lettersa-z.pcould be empty and contains only lowercase lettersa-z, and characters like.or*.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Example 3:
1 | Input: |
Example 4:
1 | Input: |
Example 5:
1 | Input: |
2.Solutions
This Solution use 2D DP. beat 90% solutions, very simple.
Here are some conditions to figure out, then the logic can be very straightforward.
1 | 1. If p.charAt(j) == s.charAt(i) : dp[i][j] = dp[i-1][j-1]; |
Here is the solution
1 | public static boolean isMatch(String s, String p) { |