Skip to content

Commit 8e20a66

Browse files
committed
8-8-2020
1 parent f0f82f2 commit 8e20a66

4 files changed

+113
-0
lines changed

src/_1496_path_crossing.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
struct Solution;
2+
3+
use std::collections::HashSet;
4+
5+
impl Solution {
6+
pub fn is_path_crossing(path: String) -> bool {
7+
let mut position: (i32, i32) = (0, 0);
8+
let mut h_set: HashSet<(i32, i32)> = HashSet::new();
9+
h_set.insert((0, 0));
10+
for h_move in path.chars() {
11+
match h_move {
12+
'N' => position.1 -= 1,
13+
'S' => position.1 += 1,
14+
'E' => position.0 += 1,
15+
'W' => position.0 -= 1,
16+
_ => {}
17+
}
18+
if !h_set.insert(position) {
19+
return true;
20+
}
21+
}
22+
return false;
23+
}
24+
}
25+
26+
#[test]
27+
fn test() {
28+
assert_eq!(
29+
Solution::is_path_crossing("NNSWWEWSSESSWENNW".to_string()),
30+
true
31+
);
32+
assert_eq!(Solution::is_path_crossing("NES".to_string()), false);
33+
assert_eq!(Solution::is_path_crossing("NESWW".to_string()), true);
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn find_kth_positive(arr: Vec<i32>, k: i32) -> i32 {
5+
let mut arr = arr;
6+
let mut k = k;
7+
let mut h_dummy: i32 = 1;
8+
let mut index: usize = 0;
9+
while k > 0 {
10+
if index < arr.len() && h_dummy == arr[index] {
11+
index += 1;
12+
} else {
13+
k -= 1;
14+
}
15+
h_dummy += 1;
16+
}
17+
h_dummy - 1
18+
}
19+
}
20+
#[test]
21+
fn test() {
22+
assert_eq!(Solution::find_kth_positive(vec![2, 3, 4, 7, 11], 5), 9);
23+
assert_eq!(Solution::find_kth_positive(vec![1, 2, 3, 4], 2), 6);
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn can_convert_string(s: String, t: String, k: i32) -> bool {
5+
let mut h_shift: Vec<i32> = vec![-1; 26];
6+
let n_s = s.len();
7+
let n_t = t.len();
8+
let s_char: Vec<char> = s.chars().collect();
9+
let t_char: Vec<char> = t.chars().collect();
10+
if n_s != n_t {
11+
return false;
12+
}
13+
14+
for i in 0..n_s {
15+
if s_char[i] == t_char[i] {
16+
continue;
17+
} else {
18+
let mut h_diff = t_char[i] as i32 - s_char[i] as i32;
19+
if h_diff < 0 {
20+
h_diff += 26;
21+
}
22+
let h_diff: usize = h_diff as usize % 26;
23+
if h_shift[h_diff] > 0 {
24+
h_shift[h_diff] += 26;
25+
} else {
26+
h_shift[h_diff] = h_diff as i32;
27+
}
28+
if h_shift[h_diff] > k {
29+
return false;
30+
}
31+
}
32+
}
33+
true
34+
}
35+
}
36+
37+
#[test]
38+
fn test() {
39+
assert_eq!(
40+
Solution::can_convert_string("input".to_string(), "output".to_string(), 9),
41+
true
42+
);
43+
assert_eq!(
44+
Solution::can_convert_string("abc".to_string(), "bcd".to_string(), 10),
45+
false
46+
);
47+
assert_eq!(
48+
Solution::can_convert_string("aab".to_string(), "bbb".to_string(), 27),
49+
true
50+
);
51+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,14 @@ mod _1304_find_n_unique_integers_sum_up_to_zero;
202202
//
203203
mod _1486_xor_operation_in_an_array;
204204
//
205+
mod _1496_path_crossing;
206+
//
205207
mod _1528_shuffle_string;
206208
//
207209
mod _1534_count_good_triplets;
208210
//
209211
mod _1535_find_the_winner_of_an_array_game;
210212
//
211213
mod _1537_get_the_maximum_score;
214+
//
215+
mod _1540_can_convert_string_in_k_moves;

0 commit comments

Comments
 (0)