Skip to content

Commit 2e10927

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 8.6 MB (73.56%)
1 parent 2be5e0c commit 2e10927

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<p>You are given a string <code>s</code> and an integer <code>k</code>. Encrypt the string using the following algorithm:</p>
2+
3+
<ul>
4+
<li>For each character <code>c</code> in <code>s</code>, replace <code>c</code> with the <code>k<sup>th</sup></code> character after <code>c</code> in the string (in a cyclic manner).</li>
5+
</ul>
6+
7+
<p>Return the <em>encrypted string</em>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<div class="example-block">
13+
<p><strong>Input:</strong> <span class="example-io">s = &quot;dart&quot;, k = 3</span></p>
14+
15+
<p><strong>Output:</strong> <span class="example-io">&quot;tdar&quot;</span></p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<ul>
20+
<li>For <code>i = 0</code>, the 3<sup>rd</sup> character after <code>&#39;d&#39;</code> is <code>&#39;t&#39;</code>.</li>
21+
<li>For <code>i = 1</code>, the 3<sup>rd</sup> character after <code>&#39;a&#39;</code> is <code>&#39;d&#39;</code>.</li>
22+
<li>For <code>i = 2</code>, the 3<sup>rd</sup> character after <code>&#39;r&#39;</code> is <code>&#39;a&#39;</code>.</li>
23+
<li>For <code>i = 3</code>, the 3<sup>rd</sup> character after <code>&#39;t&#39;</code> is <code>&#39;r&#39;</code>.</li>
24+
</ul>
25+
</div>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>Input:</strong> <span class="example-io">s = &quot;aaa&quot;, k = 1</span></p>
31+
32+
<p><strong>Output:</strong> <span class="example-io">&quot;aaa&quot;</span></p>
33+
34+
<p><strong>Explanation:</strong></p>
35+
36+
<p>As all the characters are the same, the encrypted string will also be the same.</p>
37+
</div>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
44+
<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
45+
<li><code>s</code> consists only of lowercase English letters.</li>
46+
</ul>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
string getEncryptedString(string s, int k) {
4+
int n = s.size();
5+
string ns = s;
6+
for (int i=0; i<n; i++) ns[i] = s[(i+k)%n];
7+
return ns;
8+
}
9+
};

0 commit comments

Comments
 (0)