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.
The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.).
The numerical value of some string of lowercase English letters s is the concatenation of the letter values of each letter in s, which is then converted into an integer.
s = "acb", we concatenate each letter's letter value, resulting in "021". After converting it, we get 21.You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase English letters 'a' through 'j' inclusive.
Return true if the summation of the numerical values of firstWord and secondWord equals the numerical value of targetWord, or false otherwise.
Example 1:
Input: firstWord = "acb", secondWord = "cba", targetWord = "cdb" Output: true Explanation: The numerical value of firstWord is "acb" -> "021" -> 21. The numerical value of secondWord is "cba" -> "210" -> 210. The numerical value of targetWord is "cdb" -> "231" -> 231. We return true because 21 + 210 == 231.
Example 2:
Input: firstWord = "aaa", secondWord = "a", targetWord = "aab" Output: false Explanation: The numerical value of firstWord is "aaa" -> "000" -> 0. The numerical value of secondWord is "a" -> "0" -> 0. The numerical value of targetWord is "aab" -> "001" -> 1. We return false because 0 + 0 != 1.
Example 3:
Input: firstWord = "aaa", secondWord = "a", targetWord = "aaaa" Output: true Explanation: The numerical value of firstWord is "aaa" -> "000" -> 0. The numerical value of secondWord is "a" -> "0" -> 0. The numerical value of targetWord is "aaaa" -> "0000" -> 0. We return true because 0 + 0 == 0.
Constraints:
1 <= firstWord.length, secondWord.length, targetWord.length <= 8firstWord, secondWord, and targetWord consist of lowercase English letters from 'a' to 'j' inclusive.Problem summary: The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.). The numerical value of some string of lowercase English letters s is the concatenation of the letter values of each letter in s, which is then converted into an integer. For example, if s = "acb", we concatenate each letter's letter value, resulting in "021". After converting it, we get 21. You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase English letters 'a' through 'j' inclusive. Return true if the summation of the numerical values of firstWord and secondWord equals the numerical value of targetWord, or false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"acb" "cba" "cdb"
"aaa" "a" "aab"
"aaa" "a" "aaaa"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1880: Check if Word Equals Summation of Two Words
class Solution {
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
return f(firstWord) + f(secondWord) == f(targetWord);
}
private int f(String s) {
int ans = 0;
for (char c : s.toCharArray()) {
ans = ans * 10 + (c - 'a');
}
return ans;
}
}
// Accepted solution for LeetCode #1880: Check if Word Equals Summation of Two Words
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
f := func(s string) (ans int) {
for _, c := range s {
ans = ans*10 + int(c-'a')
}
return
}
return f(firstWord)+f(secondWord) == f(targetWord)
}
# Accepted solution for LeetCode #1880: Check if Word Equals Summation of Two Words
class Solution:
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
def f(s: str) -> int:
ans, a = 0, ord("a")
for c in map(ord, s):
x = c - a
ans = ans * 10 + x
return ans
return f(firstWord) + f(secondWord) == f(targetWord)
// Accepted solution for LeetCode #1880: Check if Word Equals Summation of Two Words
impl Solution {
pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool {
fn f(s: &str) -> i64 {
let mut ans = 0;
let a = 'a' as i64;
for c in s.chars() {
let x = c as i64 - a;
ans = ans * 10 + x;
}
ans
}
f(&first_word) + f(&second_word) == f(&target_word)
}
}
// Accepted solution for LeetCode #1880: Check if Word Equals Summation of Two Words
function isSumEqual(firstWord: string, secondWord: string, targetWord: string): boolean {
const f = (s: string): number => {
let ans = 0;
for (const c of s) {
ans = ans * 10 + c.charCodeAt(0) - 97;
}
return ans;
};
return f(firstWord) + f(secondWord) == f(targetWord);
}
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.