Skip to content

feat: add rust solution to lc problem: No.0010 #1529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions solution/0000-0099/0010.Regular Expression Matching/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,49 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn is_match(s: String, p: String) -> bool {
let n = s.len();
let m = p.len();
let s = s.chars().collect::<Vec<char>>();
let p = p.chars().collect::<Vec<char>>();

let mut dp = vec![vec![false; m + 1]; n + 1];

// Initialize the dp vector
dp[0][0] = true;

for i in 1..=m {
if p[i - 1] == '*' {
dp[0][i] = dp[0][i - 2];
}
}

// Begin the actual dp process
for i in 1..=n {
for j in 1..=m {
if s[i - 1] == p[j - 1] || p[j - 1] == '.' {
dp[i][j] = dp[i - 1][j - 1];
}
if p[j - 1] == '*' {
if j >= 2 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') {
dp[i][j] = dp[i - 1][j] || dp[i][j - 2];
} else if j >= 2 && s[i - 1] != p[j - 2] {
dp[i][j] = dp[i][j - 2];
}
}
}
}

dp[n][m]
}
}
```

### **Go**

```go
Expand Down
43 changes: 43 additions & 0 deletions solution/0000-0099/0010.Regular Expression Matching/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,49 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn is_match(s: String, p: String) -> bool {
let n = s.len();
let m = p.len();
let s = s.chars().collect::<Vec<char>>();
let p = p.chars().collect::<Vec<char>>();

let mut dp = vec![vec![false; m + 1]; n + 1];

// Initialize the dp vector
dp[0][0] = true;

for i in 1..=m {
if p[i - 1] == '*' {
dp[0][i] = dp[0][i - 2];
}
}

// Begin the actual dp process
for i in 1..=n {
for j in 1..=m {
if s[i - 1] == p[j - 1] || p[j - 1] == '.' {
dp[i][j] = dp[i - 1][j - 1];
}
if p[j - 1] == '*' {
if j >= 2 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') {
dp[i][j] = dp[i - 1][j] || dp[i][j - 2];
} else if j >= 2 && s[i - 1] != p[j - 2] {
dp[i][j] = dp[i][j - 2];
}
}
}
}

dp[n][m]
}
}
```

### **Go**

```go
Expand Down
38 changes: 38 additions & 0 deletions solution/0000-0099/0010.Regular Expression Matching/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
impl Solution {
#[allow(dead_code)]
pub fn is_match(s: String, p: String) -> bool {
let n = s.len();
let m = p.len();
let s = s.chars().collect::<Vec<char>>();
let p = p.chars().collect::<Vec<char>>();

let mut dp = vec![vec![false; m + 1]; n + 1];

// Initialize the dp vector
dp[0][0] = true;

for i in 1..=m {
if p[i - 1] == '*' {
dp[0][i] = dp[0][i - 2];
}
}

// Begin the actual dp process
for i in 1..=n {
for j in 1..=m {
if s[i - 1] == p[j - 1] || p[j - 1] == '.' {
dp[i][j] = dp[i - 1][j - 1];
}
if p[j - 1] == '*' {
if j >= 2 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') {
dp[i][j] = dp[i - 1][j] || dp[i][j - 2];
} else if j >= 2 && s[i - 1] != p[j - 2] {
dp[i][j] = dp[i][j - 2];
}
}
}
}

dp[n][m]
}
}