|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Medium |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3612.Process%20String%20with%20Special%20Operations%20I/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3612. Process String with Special Operations I](https://leetcode.com/problems/process-string-with-special-operations-i) |
| 10 | + |
| 11 | +[中文文档](/solution/3600-3699/3612.Process%20String%20with%20Special%20Operations%20I/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given a string <code>s</code> consisting of lowercase English letters and the special characters: <code>*</code>, <code>#</code>, and <code>%</code>.</p> |
| 18 | + |
| 19 | +<p>Build a new string <code>result</code> by processing <code>s</code> according to the following rules from left to right:</p> |
| 20 | + |
| 21 | +<ul> |
| 22 | + <li>If the letter is a <strong>lowercase</strong> English letter append it to <code>result</code>.</li> |
| 23 | + <li>A <code>'*'</code> <strong>removes</strong> the last character from <code>result</code>, if it exists.</li> |
| 24 | + <li>A <code>'#'</code> <strong>duplicates</strong> the current <code>result</code> and <strong>appends</strong> it to itself.</li> |
| 25 | + <li>A <code>'%'</code> <strong>reverses</strong> the current <code>result</code>.</li> |
| 26 | +</ul> |
| 27 | + |
| 28 | +<p>Return the final string <code>result</code> after processing all characters in <code>s</code>.</p> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong class="example">Example 1:</strong></p> |
| 32 | + |
| 33 | +<div class="example-block"> |
| 34 | +<p><strong>Input:</strong> <span class="example-io">s = "a#b%*"</span></p> |
| 35 | + |
| 36 | +<p><strong>Output:</strong> <span class="example-io">"ba"</span></p> |
| 37 | + |
| 38 | +<p><strong>Explanation:</strong></p> |
| 39 | + |
| 40 | +<table style="border: 1px solid black;"> |
| 41 | + <thead> |
| 42 | + <tr> |
| 43 | + <th style="border: 1px solid black;"><code>i</code></th> |
| 44 | + <th style="border: 1px solid black;"><code>s[i]</code></th> |
| 45 | + <th style="border: 1px solid black;">Operation</th> |
| 46 | + <th style="border: 1px solid black;">Current <code>result</code></th> |
| 47 | + </tr> |
| 48 | + </thead> |
| 49 | + <tbody> |
| 50 | + <tr> |
| 51 | + <td style="border: 1px solid black;">0</td> |
| 52 | + <td style="border: 1px solid black;"><code>'a'</code></td> |
| 53 | + <td style="border: 1px solid black;">Append <code>'a'</code></td> |
| 54 | + <td style="border: 1px solid black;"><code>"a"</code></td> |
| 55 | + </tr> |
| 56 | + <tr> |
| 57 | + <td style="border: 1px solid black;">1</td> |
| 58 | + <td style="border: 1px solid black;"><code>'#'</code></td> |
| 59 | + <td style="border: 1px solid black;">Duplicate <code>result</code></td> |
| 60 | + <td style="border: 1px solid black;"><code>"aa"</code></td> |
| 61 | + </tr> |
| 62 | + <tr> |
| 63 | + <td style="border: 1px solid black;">2</td> |
| 64 | + <td style="border: 1px solid black;"><code>'b'</code></td> |
| 65 | + <td style="border: 1px solid black;">Append <code>'b'</code></td> |
| 66 | + <td style="border: 1px solid black;"><code>"aab"</code></td> |
| 67 | + </tr> |
| 68 | + <tr> |
| 69 | + <td style="border: 1px solid black;">3</td> |
| 70 | + <td style="border: 1px solid black;"><code>'%'</code></td> |
| 71 | + <td style="border: 1px solid black;">Reverse <code>result</code></td> |
| 72 | + <td style="border: 1px solid black;"><code>"baa"</code></td> |
| 73 | + </tr> |
| 74 | + <tr> |
| 75 | + <td style="border: 1px solid black;">4</td> |
| 76 | + <td style="border: 1px solid black;"><code>'*'</code></td> |
| 77 | + <td style="border: 1px solid black;">Remove the last character</td> |
| 78 | + <td style="border: 1px solid black;"><code>"ba"</code></td> |
| 79 | + </tr> |
| 80 | + </tbody> |
| 81 | +</table> |
| 82 | + |
| 83 | +<p>Thus, the final <code>result</code> is <code>"ba"</code>.</p> |
| 84 | +</div> |
| 85 | + |
| 86 | +<p><strong class="example">Example 2:</strong></p> |
| 87 | + |
| 88 | +<div class="example-block"> |
| 89 | +<p><strong>Input:</strong> <span class="example-io">s = "z*#"</span></p> |
| 90 | + |
| 91 | +<p><strong>Output:</strong> <span class="example-io">""</span></p> |
| 92 | + |
| 93 | +<p><strong>Explanation:</strong></p> |
| 94 | + |
| 95 | +<table style="border: 1px solid black;"> |
| 96 | + <thead> |
| 97 | + <tr> |
| 98 | + <th style="border: 1px solid black;"><code>i</code></th> |
| 99 | + <th style="border: 1px solid black;"><code>s[i]</code></th> |
| 100 | + <th style="border: 1px solid black;">Operation</th> |
| 101 | + <th style="border: 1px solid black;">Current <code>result</code></th> |
| 102 | + </tr> |
| 103 | + </thead> |
| 104 | + <tbody> |
| 105 | + <tr> |
| 106 | + <td style="border: 1px solid black;">0</td> |
| 107 | + <td style="border: 1px solid black;"><code>'z'</code></td> |
| 108 | + <td style="border: 1px solid black;">Append <code>'z'</code></td> |
| 109 | + <td style="border: 1px solid black;"><code>"z"</code></td> |
| 110 | + </tr> |
| 111 | + <tr> |
| 112 | + <td style="border: 1px solid black;">1</td> |
| 113 | + <td style="border: 1px solid black;"><code>'*'</code></td> |
| 114 | + <td style="border: 1px solid black;">Remove the last character</td> |
| 115 | + <td style="border: 1px solid black;"><code>""</code></td> |
| 116 | + </tr> |
| 117 | + <tr> |
| 118 | + <td style="border: 1px solid black;">2</td> |
| 119 | + <td style="border: 1px solid black;"><code>'#'</code></td> |
| 120 | + <td style="border: 1px solid black;">Duplicate the string</td> |
| 121 | + <td style="border: 1px solid black;"><code>""</code></td> |
| 122 | + </tr> |
| 123 | + </tbody> |
| 124 | +</table> |
| 125 | + |
| 126 | +<p>Thus, the final <code>result</code> is <code>""</code>.</p> |
| 127 | +</div> |
| 128 | + |
| 129 | +<p> </p> |
| 130 | +<p><strong>Constraints:</strong></p> |
| 131 | + |
| 132 | +<ul> |
| 133 | + <li><code>1 <= s.length <= 20</code></li> |
| 134 | + <li><code>s</code> consists of only lowercase English letters and special characters <code>*</code>, <code>#</code>, and <code>%</code>.</li> |
| 135 | +</ul> |
| 136 | + |
| 137 | +<!-- description:end --> |
| 138 | + |
| 139 | +## Solutions |
| 140 | + |
| 141 | +<!-- solution:start --> |
| 142 | + |
| 143 | +### Solution 1 |
| 144 | + |
| 145 | +<!-- tabs:start --> |
| 146 | + |
| 147 | +#### Python3 |
| 148 | + |
| 149 | +```python |
| 150 | + |
| 151 | +``` |
| 152 | + |
| 153 | +#### Java |
| 154 | + |
| 155 | +```java |
| 156 | + |
| 157 | +``` |
| 158 | + |
| 159 | +#### C++ |
| 160 | + |
| 161 | +```cpp |
| 162 | + |
| 163 | +``` |
| 164 | + |
| 165 | +#### Go |
| 166 | + |
| 167 | +```go |
| 168 | + |
| 169 | +``` |
| 170 | + |
| 171 | +<!-- tabs:end --> |
| 172 | + |
| 173 | +<!-- solution:end --> |
| 174 | + |
| 175 | +<!-- problem:end --> |
0 commit comments