Skip to content

Commit b015b95

Browse files
committed
12/7/2020
1 parent ceed131 commit b015b95

File tree

3 files changed

+50
-52
lines changed

3 files changed

+50
-52
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ leetcode practise solution in rust
3131
|70|[Climbing Stairs](src/_0070_climbing_stairs.rs)|
3232
|74|[Search a 2D Matrix](src/_0074_search_a_2d_matrix.rs)|
3333
|78|[Subsets](src/_0078_subsets.rs)|
34-
|79|[Word Search](src/_0079_word_search.rs)|
34+
|79|[Word Search](src/_0079_word_search)|
3535
|83|[Remove Duplicates from Sorted List](src/_0083_remove_duplicates_from_sorted_list.rs)|
3636
|88|[Merge Sorted Array](src/_0088_merge_sorted_array.rs)|
3737
|91|[Decode Ways](src/_0091_decode_ways.rs)|
@@ -146,6 +146,7 @@ leetcode practise solution in rust
146146
|1539|[Kth Missing Positive Number](src/_1539_kth_missing_positive_number.rs)|
147147
|1540|[Can Convert String in K Moves](src/_1540_can_convert_string_in_k_moves.rs)|
148148
|1572|[Matrix Diagonal Sum ](src/_1572_matrix_diagonal_sum.rs)|
149+
|1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](src/_1576_replace_all_question_mark_to_avoid_consecutive_repeating_characters.rs)|
149150
|1577|[Number of Ways Where Square of Number Is Equal to Product of Two Numbers](src/_1577_number_of_ways_where_square_of_number_is_equal_to_product_of_two_numbers.rs)|
150151
|1582|[Special Positions in a Binary Matrix](src/_1582_special_positions_in_a_binary_matrix.rs)|
151152
|1583|[Count Unhappy Friends](src/_1583_count_unhappy_friends.rs)|
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
struct Solution;
22
impl Solution {
33
pub fn modify_string(s: String) -> String {
4-
let mut result: Vec<char> = vec![];
5-
let mut char_fill: u8 = 0;
6-
let s: Vec<char> = s.chars().collect();
7-
for (i, v) in s.iter().enumerate() {
8-
match v {
9-
'?' => {
10-
let mut potential_char = (char_fill + b'a') as char;
11-
while (i > 0 && potential_char == result[i - 1])
12-
|| (i < s.len() - 1 && potential_char == s[i + 1])
13-
{
14-
if char_fill == 27 {
15-
char_fill = 0;
16-
}
17-
char_fill += 1;
18-
potential_char = (char_fill + b'a') as char;
4+
let mut result = s.as_bytes().to_owned();
5+
let mut ch = 0;
6+
7+
for i in 0..s.len() {
8+
if result[i] == '?' as u8 {
9+
if i == 0 {
10+
if s.len() != 1 && result[1] == 'a' as u8 {
11+
ch += 1;
1912
}
20-
result.push(potential_char);
21-
if char_fill == 27 {
22-
char_fill = 0;
13+
} else if i == s.len() - 1 {
14+
ch = (ch + 1) % 26;
15+
} else {
16+
while result[i + 1] == 'a' as u8 + ch || result[i - 1] == 'a' as u8 + ch {
17+
ch = (ch + 1) % 26;
2318
}
24-
char_fill += 1;
2519
}
26-
_ => result.push(v.clone()),
20+
21+
result[i] = 'a' as u8 + ch;
22+
} else {
23+
ch = (result[i] as u8 - 'a' as u8 + 1) % 26;
2724
}
2825
}
29-
dbg!(&result);
30-
result.into_iter().collect()
26+
27+
std::str::from_utf8(&result).unwrap().to_string()
3128
}
3229
}
3330
#[test]
@@ -38,14 +35,14 @@ fn test() {
3835
);
3936
assert_eq!(
4037
Solution::modify_string("ubv?w".to_string()),
41-
"ubvaw".to_string()
38+
"ubvxw".to_string()
4239
);
4340
assert_eq!(
4441
Solution::modify_string("j?qg??b".to_string()),
45-
"jaqgbcb".to_string()
42+
"jkqghib".to_string()
4643
);
4744
assert_eq!(
4845
Solution::modify_string("??yw?ipkj?".to_string()),
49-
"abywcipkjd".to_string()
46+
"abywxipkjl".to_string()
5047
);
5148
}
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
// struct Solution;
1+
struct Solution;
22

3-
// impl Solution {
4-
// pub fn concatenated_binary(n: i32) -> i32 {
5-
// let mut result: usize = 0;
6-
// for i in 1..=n as usize {
7-
// result <<= Self::binary_digits(i);
8-
// result += i;
9-
// result %= 1_000_000_007;
10-
// }
11-
// result as i32
12-
// }
3+
impl Solution {
4+
pub fn concatenated_binary(n: i32) -> i32 {
5+
let mut result: usize = 0;
6+
for i in 1..=n as usize {
7+
result <<= Self::binary_digits(i);
8+
result += i;
9+
result %= 1_000_000_007;
10+
}
11+
result as i32
12+
}
1313

14-
// fn binary_digits(mut decimal: usize) -> usize {
15-
// let mut bits = 0;
16-
// while decimal > 0 {
17-
// bits += 1;
18-
// decimal >> 1;
19-
// }
20-
// bits
21-
// }
22-
// }
14+
fn binary_digits(mut decimal: usize) -> usize {
15+
let mut bits = 0;
16+
while decimal > 0 {
17+
bits += 1;
18+
decimal >> 1;
19+
}
20+
bits
21+
}
22+
}
2323

24-
// #[test]
25-
// fn test() {
26-
// assert_eq!(Solution::concatenated_binary(1), 1);
27-
// assert_eq!(Solution::concatenated_binary(3), 3);
28-
// assert_eq!(Solution::concatenated_binary(12), 505379714);
29-
// }
24+
#[test]
25+
fn test() {
26+
// assert_eq!(Solution::concatenated_binary(1), 1);
27+
// assert_eq!(Solution::concatenated_binary(3), 3);
28+
// assert_eq!(Solution::concatenated_binary(12), 505379714);
29+
}

0 commit comments

Comments
 (0)