In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use regular expressions in leetcode, which is very detailed and has certain reference value. Friends who are interested must read it!
Given a string (s) and a character pattern (p). Implement support for'.' Matches the regular expression of'*'.
'.' Matches any single character.
'*' matches zero or more preceding elements.
The match should cover the entire string (s), not part of the string.
Description:
S may be empty and contain only lowercase letters from amurz.
P may be empty and contain only lowercase letters from Amurz, as well as characters. And *.
Example 1:
Enter:
S = "aa"
P = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:
Enter:
S = "aa"
P = "a *"
Output: true
Explanation:'* 'means to match zero or more preceding elements, that is, to match' a'. Therefore, repeat'a 'once and the string can be changed to "aa".
Example 3:
Enter:
S = "ab"
P = ". *"
Output: true
Explanation: ". *" means to match zero or more ('*') any character ('.').
Example 4:
Enter:
S = "aab"
P = "c*a*b"
Output: true
Explanation:'c 'can not be repeated,' a 'can be repeated. So you can match the string "aab".
Example 5:
Enter:
S = "mississippi"
P = "mis*is*p*."
Output: false
Ideas for solving the problem:
1, whether the two characters match only needs to be judged by s [I] = = p [j] | | p [j] = ='.
2. In the case of p [j]! ='*', the matching condition between the character before p [j] and that before s [I] is that the characters before p [j] = = s [I] & & p [j-1] and s [I-1] match
3, p [j] ='*'is divided into three cases
(1), p [j-1] match 0 times & & p [j-2] and s [I] match
(2), p [j-1] match once (p [j-1] = = s [I]) & & p [j-2] and s [I-1] match
(3), p [j-1] matches many times, p [j-1] = = s [I]
A, p [j] and s [I-1] match
B, p [j-1] and s [I] match
4. Save the intermediate results with the array a [len (s) + 1] [len (p) + 1], where a [I] [j] indicates whether s [0RV I] and p [0RV j] match
5, since the case of c * can represent 0 times, for convenience, adding 1 to the length of I and j indicates that the empty string matches each other.
6. There are many initial conditions:
A [0] [0] = true / / empty and empty match
A [0] [2qk] = a [0] [2* (kmurl 1)] & & p [2 * kmurl 1] = ='*'/ / c* this kind of match 0 times
Func isMatch (s string, p string) bool {if len (s) = = 0 & & len (p) = = 0 {return true} if len (p) = = 0 {return false} if len (s) = 0 {if len (p)% 2! = 0 {return false} for I: = 1; I < len (p) I + = 2 {if [] byte (p) [I]! ='*'{return false}} return true} bs: = [] byte (s) bp: = [] byte (p) a: = make ([] [] bool, len (s) + 1) for I: = 0; I < len (s) + 1 For + {a [I] = make ([] bool, len (p) + 1)} a [0] [0] = true a [1] [1] = match (bs [0], bp [0]) for j: = 2; j < len (p) + 1; j = j + 2 {a [0] [j] = a [0] [jmur2] & & BP [j-1] = ='*'}
For I: = 1; I < len (s) + 1; iTunes + {for j: = 2; j < len (p) + 1 Jake + {if bp [j-1] ='*'{/ / 01a [I] [j] = a [I] [jmur2] | (a [I-1] [jmur1] & & match (bs [I-1], bp [j-2])) | | (a [I] [jMust1] & match (Bs [I-1]) BP [j-2])) | | (a [I-1] [j] & & match (BS [I-1], BP [j-2]))} else {a [I] [j] = a [I-1] [Jmur1] & & match (BS [I-1], BP [j-1])}
Return a [len (s)] [len (p)]
}
Func match (a, b byte) bool {return a = = b | | b ='.'} these are all the contents of the article "how leetcode uses regular expressions". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.