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.
Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.
Example 1:
Input: s = "Hello" Output: "hello"
Example 2:
Input: s = "here" Output: "here"
Example 3:
Input: s = "LOVELY" Output: "lovely"
Constraints:
1 <= s.length <= 100s consists of printable ASCII characters.Problem summary: Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"Hello"
"here"
"LOVELY"
capitalize-the-title)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #709: To Lower Case
class Solution {
public String toLowerCase(String s) {
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; ++i) {
if (cs[i] >= 'A' && cs[i] <= 'Z') {
cs[i] |= 32;
}
}
return String.valueOf(cs);
}
}
// Accepted solution for LeetCode #709: To Lower Case
func toLowerCase(s string) string {
cs := []byte(s)
for i, c := range cs {
if c >= 'A' && c <= 'Z' {
cs[i] |= 32
}
}
return string(cs)
}
# Accepted solution for LeetCode #709: To Lower Case
class Solution:
def toLowerCase(self, s: str) -> str:
return "".join([chr(ord(c) | 32) if c.isupper() else c for c in s])
// Accepted solution for LeetCode #709: To Lower Case
impl Solution {
pub fn to_lower_case(s: String) -> String {
s.to_ascii_lowercase()
}
}
// Accepted solution for LeetCode #709: To Lower Case
function toLowerCase(s: string): string {
return s.toLowerCase();
}
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.