Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of regular expression matching by LeetCode

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces LeetCode how to solve the regular expression matching problem, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Title

Please implement a function to match the containing'. Regular expressions for 'and'. The character'.'in the pattern Represents any character, and''means that the character before it can appear any number of times (including 0 times). In this case, matching means that all characters of a string match the entire pattern. For example, the string "aaa" matches the patterns "A.A" and "abaca", but not "aa.a" and "ab*a".

Example 1: input: s = "aa" p = "a" output: false explains: "a" does not match the entire string "aa". Example 2: input: s = "aa" p = "a *" output: true explains: because'* 'represents the first element that can match zero or more, the preceding element here is' a'. Therefore, the string "aa" can be regarded as'a 'repeated once. Example 3: input: s = "ab" p = ". *" output: true explanation: ". *" means to match zero or more ('*') any character ('.'). Example 4: input: s = "aab" p = "c*a*b" output: true explanation: because'* 'means zero or more, where' c'is 0 and'a'is repeated. So you can match the string "aab". Example 5: enter: s = "mississippi" p = "mis*is*p*." Output: falses may be empty and contain only lowercase letters from Amurz. P may be empty and contain only lowercase letters and characters from Amurz. And *, there is no continuous'*'. Train of thought

If the last character of BB is a normal character, it is to see whether A [n-1] is equal to B [m-1]. If it is equal, it will look at A _ {0..n-2} and B _ {0..m-2}. If it is not equal, it will not match. This is a sub-problem.

If the last character of BB is'.', it can match any character. Look directly at A _ {0..n-2} and B _ {0..m-2}

If the last character of B is', it means that B [m-2] = c can be repeated 0 or more times, they are a whole c

Case 1: a [n-1] is 0 c 0..n-1 B the last two characters are invalid, whether it can match depends on whether A _ {0..m-3} and B _ {match} match.

Case 2: a [n-1] is the last of multiple cs (this case must be A [n-1] = c or clock.'), so A [n-1] moves forward one after the match, and B continues to match, because there can be multiple matches. Continue to see if A _ {0..n-2} and B _ {0..m-1} match.

Code class Solution {public boolean isMatch (String s, String p) {int n = s.length (), m = p.length (); boolean [] [] f = new boolean [nong1] [mCl1]; for (int I = 0; I = 2) {f [I] [j] | = f [I] [Jmur2] } if (I > = 1 & & j > = 2 & & (s.charAt (iMuth1) = = p.charAt (jmur2) | | p.charAt (jmur2) = ='.') {f [I] [j] | = f [I-1] [j] } return f [n] [m] }} Thank you for reading this article carefully. I hope the article "LeetCode how to solve the regular expression matching problem" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report