Skip to content

Commit b9dda91

Browse files
committed
Rust
1 parent 842d342 commit b9dda91

6 files changed

+131
-0
lines changed

longest-happy-string.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Longest Happy String
2+
impl Solution {
3+
pub fn longest_diverse_string(a: i32, b: i32, c: i32) -> String {
4+
use std::mem::swap;
5+
let mut ans = vec![];
6+
let mut s = vec![a, b, c];
7+
let mut l2 = 3;
8+
let mut l1 = 3;
9+
while s[0]+s[1]+s[2] > 0 {
10+
let mut x = 0; let mut y = 1; let mut z = 2;
11+
if s[x] < s[y] { swap(&mut x, &mut y); }
12+
if s[y] < s[z] { swap(&mut y, &mut z); }
13+
if s[x] < s[y] { swap(&mut x, &mut y); }
14+
if x != l2 || x != l1 {}
15+
else if s[y] > 0 { x = y; }
16+
else { break; }
17+
ans.push(b'a'+x as u8);
18+
s[x] -= 1;
19+
l2 = l1;
20+
l1 = x;
21+
}
22+
String::from_utf8_lossy(&ans).into_owned()
23+
}
24+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Minimum Add to Make Parentheses Valid
2+
impl Solution {
3+
pub fn min_add_to_make_valid(s: String) -> i32 {
4+
let mut l = 0;
5+
let mut r = 0;
6+
for &c in s.as_bytes().iter() {
7+
if c == b'(' {
8+
l += 1;
9+
} else if l > 0 {
10+
l -= 1;
11+
} else {
12+
r += 1;
13+
}
14+
}
15+
l+r
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Minimum Insertions to Balance a Parentheses String
2+
impl Solution {
3+
pub fn min_insertions(s: String) -> i32 {
4+
let mut ans = 0;
5+
let mut x = 0;
6+
for &c in s.as_bytes().iter() {
7+
if c == b'(' {
8+
if x%2 == 1 {
9+
x -= 1;
10+
ans += 1;
11+
}
12+
x += 2;
13+
} else {
14+
if x == 0 {
15+
x += 2;
16+
ans += 1;
17+
}
18+
x -= 1;
19+
}
20+
}
21+
ans+x
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Minimum Number of Swaps to Make the String Balanced
2+
impl Solution {
3+
pub fn min_swaps(s: String) -> i32 {
4+
let mut x = 0;
5+
for &c in s.as_bytes().iter() {
6+
if c == b'[' {
7+
x += 1;
8+
} else if x > 0 {
9+
x -= 1;
10+
}
11+
}
12+
(x+1)/2
13+
}
14+
}

remove-invalid-parentheses.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn remove_invalid_parentheses(s: String) -> Vec<String> {
3+
fn dfs(s: Vec<u8>, ans: &mut Vec<String>, par: u8, ii: usize, jj: usize) {
4+
let mut c = 0i32;
5+
for i in ii..s.len() {
6+
if s[i] == par { c += 1; }
7+
else if s[i] == (par^1) { c -= 1; }
8+
if c < 0 {
9+
for j in jj..=i {
10+
if s[j] == (par^1) && (j == jj || s[j-1] != (par^1)) {
11+
let mut s1 = s.clone();
12+
s1.remove(j);
13+
dfs(s1, ans, par, i, j);
14+
}
15+
}
16+
return;
17+
}
18+
}
19+
let mut s1 = s.clone();
20+
s1.reverse();
21+
if par == b'(' {
22+
dfs(s1, ans, par^1, 0, 0);
23+
} else {
24+
ans.push(String::from_utf8_lossy(&s1).into_owned());
25+
}
26+
}
27+
let mut ans = vec![];
28+
dfs(Vec::from(s.as_bytes()), &mut ans, b'(', 0, 0);
29+
ans
30+
}
31+
}

valid-parenthesis-string.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Valid Parenthesis String
2+
impl Solution {
3+
pub fn check_valid_string(s: String) -> bool {
4+
let mut mn = 0i32;
5+
let mut mx = 0i32;
6+
for &c in s.as_bytes().iter() {
7+
if c == b'(' {
8+
mn += 1;
9+
mx += 1;
10+
} else if c == b')' {
11+
mn -= 1;
12+
mx -= 1;
13+
} else {
14+
mn -= 1;
15+
mx += 1;
16+
}
17+
if mx < 0 { return false; }
18+
mn = mn.max(0);
19+
}
20+
mn == 0
21+
}
22+
}

0 commit comments

Comments
 (0)