|
| 1 | +/** |
| 2 | + * [2000] Reverse Prefix of Word |
| 3 | + * |
| 4 | + * Given a 0-indexed string word and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing. |
| 5 | + * |
| 6 | + * For example, if word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be "<u>dcba</u>efd". |
| 7 | + * |
| 8 | + * Return the resulting string. |
| 9 | + * |
| 10 | + * Example 1: |
| 11 | + * |
| 12 | + * Input: word = "<u>abcd</u>efd", ch = "d" |
| 13 | + * Output: "<u>dcba</u>efd" |
| 14 | + * Explanation: The first occurrence of "d" is at index 3. |
| 15 | + * Reverse the part of word from 0 to 3 (inclusive), the resulting string is "dcbaefd". |
| 16 | + * |
| 17 | + * Example 2: |
| 18 | + * |
| 19 | + * Input: word = "<u>xyxz</u>xe", ch = "z" |
| 20 | + * Output: "<u>zxyx</u>xe" |
| 21 | + * Explanation: The first and only occurrence of "z" is at index 3. |
| 22 | + * Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe". |
| 23 | + * |
| 24 | + * Example 3: |
| 25 | + * |
| 26 | + * Input: word = "abcd", ch = "z" |
| 27 | + * Output: "abcd" |
| 28 | + * Explanation: "z" does not exist in word. |
| 29 | + * You should not do any reverse operation, the resulting string is "abcd". |
| 30 | + * |
| 31 | + * |
| 32 | + * Constraints: |
| 33 | + * |
| 34 | + * 1 <= word.length <= 250 |
| 35 | + * word consists of lowercase English letters. |
| 36 | + * ch is a lowercase English letter. |
| 37 | + * |
| 38 | + */ |
| 39 | +pub struct Solution {} |
| 40 | + |
| 41 | +// problem: https://leetcode.com/problems/reverse-prefix-of-word/ |
| 42 | +// discuss: https://leetcode.com/problems/reverse-prefix-of-word/discuss/?currentPage=1&orderBy=most_votes&query= |
| 43 | + |
| 44 | +// submission codes start here |
| 45 | + |
| 46 | +impl Solution { |
| 47 | + pub fn reverse_prefix(word: String, ch: char) -> String { |
| 48 | + match word.split_once(ch) { |
| 49 | + Some((pre, post)) => ch.to_string() + &pre.chars().rev().collect::<String>() + post, |
| 50 | + None => word, |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// submission codes end |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + use super::*; |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn test_2000_example_1() { |
| 63 | + let word = "abcdefd".to_string(); |
| 64 | + let ch = 'd'; |
| 65 | + |
| 66 | + let result = "dcbaefd".to_string(); |
| 67 | + |
| 68 | + assert_eq!(Solution::reverse_prefix(word, ch), result); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn test_2000_example_2() { |
| 73 | + let word = "xyxzxe".to_string(); |
| 74 | + let ch = 'z'; |
| 75 | + |
| 76 | + let result = "zxyxxe".to_string(); |
| 77 | + |
| 78 | + assert_eq!(Solution::reverse_prefix(word, ch), result); |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_2000_example_3() { |
| 83 | + let word = "abcd".to_string(); |
| 84 | + let ch = 'z'; |
| 85 | + |
| 86 | + let result = "abcd".to_string(); |
| 87 | + |
| 88 | + assert_eq!(Solution::reverse_prefix(word, ch), result); |
| 89 | + } |
| 90 | +} |
0 commit comments