aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-328/benjamin-andre/README1
-rwxr-xr-xchallenge-328/benjamin-andre/rust/ch-1.rs25
-rwxr-xr-xchallenge-328/benjamin-andre/rust/ch-2.rs23
3 files changed, 49 insertions, 0 deletions
diff --git a/challenge-328/benjamin-andre/README b/challenge-328/benjamin-andre/README
new file mode 100644
index 0000000000..1ba71899bc
--- /dev/null
+++ b/challenge-328/benjamin-andre/README
@@ -0,0 +1 @@
+Solution by Benjamin Andre.
diff --git a/challenge-328/benjamin-andre/rust/ch-1.rs b/challenge-328/benjamin-andre/rust/ch-1.rs
new file mode 100755
index 0000000000..49fa320dc9
--- /dev/null
+++ b/challenge-328/benjamin-andre/rust/ch-1.rs
@@ -0,0 +1,25 @@
+#!/bin/sh
+//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit
+
+fn replace(s: &str) -> String {
+ let mut res: Vec<char> = s.chars().collect();
+ let n = res.len();
+ for i in 0..n {
+ if res[i] == '?' {
+ for c in 'a'..='z' {
+ if (i == 0 || res[i - 1] != c) && (i == n - 1 || res[i + 1] != c) {
+ res[i] = c;
+ break;
+ }
+ }
+ }
+ }
+ res.into_iter().collect()
+}
+
+#[test]
+fn example() {
+ assert_eq!(replace("a?z"), "abz");
+ assert_eq!(replace("pe?k"), "peak");
+ assert_eq!(replace("gra?te"), "grabte");
+}
diff --git a/challenge-328/benjamin-andre/rust/ch-2.rs b/challenge-328/benjamin-andre/rust/ch-2.rs
new file mode 100755
index 0000000000..c2c4c9bf19
--- /dev/null
+++ b/challenge-328/benjamin-andre/rust/ch-2.rs
@@ -0,0 +1,23 @@
+#!/bin/sh
+//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit
+
+fn good_string(s: &str) -> String {
+ let mut stack: Vec<char> = Vec::new();
+ for c in s.chars() {
+ if let Some(&top) = stack.last() {
+ if top != c && top.eq_ignore_ascii_case(&c) {
+ stack.pop();
+ continue;
+ }
+ }
+ stack.push(c);
+ }
+ stack.into_iter().collect()
+}
+
+#[test]
+fn example() {
+ assert_eq!(good_string("WeEeekly"), "Weekly");
+ assert_eq!(good_string("abBAdD"), "");
+ assert_eq!(good_string("abc"), "abc");
+}