Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
widmonstertony
Leetcode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Leetcode
/
java
/
10.regular-expression-matching.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
54 lines (52 loc) · 2.27 KB
master
Breadcrumbs
Leetcode
/
java
/
10.regular-expression-matching.java
Copy path
Top
File metadata and controls
Code
Blame
54 lines (52 loc) · 2.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* @lc app=leetcode id=10 lang=java
*
* [10] Regular Expression Matching
*/
// @lc code=start
class Solution {
public boolean isMatch(String s, String p) {
if (p.isEmpty()) {
return s.isEmpty();
}
if (p.length() == 1) {
return s.length() == 1 && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.');
}
// 若p的第二个字符不为*
if (p.charAt(1) != '*') {
// 若此时s为空返回 false
if(s.isEmpty()) {
return false;
}
// 否则判断首字符是否匹配,且从各自的第二个字符开始调用递归函数匹配。
else {
return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.' )
&& isMatch(s.substring(1), p.substring(1));
}
}
// 若p的第二个字符为*,进行下列循环
// 条件是若s不为空且首字符匹配(包括 p[0] 为点)
// 调用递归函数匹配s和去掉前两个字符的p
while (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')) {
// 这样做的原因是假设此时的星号的作用是让前面的字符出现0次,验证是否匹配
if (isMatch(s, p.substring(2))) {
return true;
}
// 如果不能匹配,那么就要用*去匹配s的第一个字母
// 因为此时首字母匹配了,我们可以去掉s的首字母
// 而p由于星号的作用,可以有任意个首字母,所以不需要去掉
else {
s = s.substring(1);
}
}
// 返回调用递归函数匹配s和去掉前两个字符的p的结果
//(这么做的原因是处理星号无法匹配的内容
// 比如 s="ab", p="a*b",直接进入 while 循环后
// 我们发现 "ab" 和 "b" 不匹配,所以s变成 "b"
// 那么此时跳出循环后,就到最后的 return 来比较 "b" 和 "b" 了
// 返回 true。再举个例子,比如 s="", p="a*",由于s为空,
// 不会进入任何的 if 和 while,只能到最后的 return 来比较了,返回 true,正确)
return isMatch(s, p.substring(2));
}
}
// @lc code=end
You can’t perform that action at this time.