Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
You are given a string s and an integer k. Encrypt the string using the following algorithm:
c in s, replace c with the kth character after c in the string (in a cyclic manner).Return the encrypted string.
Example 1:
Input: s = "dart", k = 3
Output: "tdar"
Explanation:
i = 0, the 3rd character after 'd' is 't'.i = 1, the 3rd character after 'a' is 'd'.i = 2, the 3rd character after 'r' is 'a'.i = 3, the 3rd character after 't' is 'r'.Example 2:
Input: s = "aaa", k = 1
Output: "aaa"
Explanation:
As all the characters are the same, the encrypted string will also be the same.
Constraints:
1 <= s.length <= 1001 <= k <= 104s consists only of lowercase English letters.Problem summary: You are given a string s and an integer k. Encrypt the string using the following algorithm: For each character c in s, replace c with the kth character after c in the string (in a cyclic manner). Return the encrypted string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"dart" 3
"aaa" 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3210: Find the Encrypted String
class Solution {
public String getEncryptedString(String s, int k) {
char[] cs = s.toCharArray();
int n = cs.length;
for (int i = 0; i < n; ++i) {
cs[i] = s.charAt((i + k) % n);
}
return new String(cs);
}
}
// Accepted solution for LeetCode #3210: Find the Encrypted String
func getEncryptedString(s string, k int) string {
cs := []byte(s)
for i := range s {
cs[i] = s[(i+k)%len(s)]
}
return string(cs)
}
# Accepted solution for LeetCode #3210: Find the Encrypted String
class Solution:
def getEncryptedString(self, s: str, k: int) -> str:
cs = list(s)
n = len(s)
for i in range(n):
cs[i] = s[(i + k) % n]
return "".join(cs)
// Accepted solution for LeetCode #3210: Find the Encrypted String
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3210: Find the Encrypted String
// class Solution {
// public String getEncryptedString(String s, int k) {
// char[] cs = s.toCharArray();
// int n = cs.length;
// for (int i = 0; i < n; ++i) {
// cs[i] = s.charAt((i + k) % n);
// }
// return new String(cs);
// }
// }
// Accepted solution for LeetCode #3210: Find the Encrypted String
function getEncryptedString(s: string, k: number): string {
const cs: string[] = [];
const n = s.length;
for (let i = 0; i < n; ++i) {
cs[i] = s[(i + k) % n];
}
return cs.join('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.