Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5182064

Browse files
committed
Create 0010-regular-expression-matching.swift
1 parent468d578 commit5182064

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
classSolution{
2+
func isMatch(_ s:String, _ p:String)->Bool{
3+
lets=Array(s),p=Array(p)
4+
vardp=[[Int]: Bool]()
5+
6+
func dfs(_ i:Int, _ j:Int)->Bool{
7+
ifdp[[i, j]]!=nil{
8+
returndp[[i, j]]!
9+
}
10+
if i>= s.count && j>= p.count{
11+
returntrue
12+
}
13+
if j>= p.count{
14+
returnfalse
15+
}
16+
letisMatch= i< s.count &&(s[i]==p[j] ||p[j]==".")
17+
if j+1< p.count &&p[j+1]=="*"{
18+
dp[[i, j]]=dfs(i, j+2) ||(isMatch &&dfs(i+1, j))
19+
returndp[[i, j]]!
20+
}
21+
if isMatch{
22+
dp[[i, j]]=dfs(i+1, j+1)
23+
returndp[[i, j]]!
24+
}
25+
dp[[i, j]]=false
26+
returnfalse
27+
}
28+
29+
returndfs(0,0)
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp