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 array fundamentals.
You are given an array of equal-length strings words. Assume that the length of each string is n.
Each string words[i] can be converted into a difference integer array difference[i] of length n - 1 where difference[i][j] = words[i][j+1] - words[i][j] where 0 <= j <= n - 2. Note that the difference between two letters is the difference between their positions in the alphabet i.e. the position of 'a' is 0, 'b' is 1, and 'z' is 25.
"acb", the difference integer array is [2 - 0, 1 - 2] = [2, -1].All the strings in words have the same difference integer array, except one. You should find that string.
Return the string in words that has different difference integer array.
Example 1:
Input: words = ["adc","wzy","abc"] Output: "abc" Explanation: - The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1]. - The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1]. - The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1]. The odd array out is [1, 1], so we return the corresponding string, "abc".
Example 2:
Input: words = ["aaa","bob","ccc","ddd"] Output: "bob" Explanation: All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
Constraints:
3 <= words.length <= 100n == words[i].length2 <= n <= 20words[i] consists of lowercase English letters.Problem summary: You are given an array of equal-length strings words. Assume that the length of each string is n. Each string words[i] can be converted into a difference integer array difference[i] of length n - 1 where difference[i][j] = words[i][j+1] - words[i][j] where 0 <= j <= n - 2. Note that the difference between two letters is the difference between their positions in the alphabet i.e. the position of 'a' is 0, 'b' is 1, and 'z' is 25. For example, for the string "acb", the difference integer array is [2 - 0, 1 - 2] = [2, -1]. All the strings in words have the same difference integer array, except one. You should find that string. Return the string in words that has different difference integer array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["adc","wzy","abc"]
["aaa","bob","ccc","ddd"]
minimum-rounds-to-complete-all-tasks)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2451: Odd String Difference
class Solution {
public String oddString(String[] words) {
var d = new HashMap<String, List<String>>();
for (var s : words) {
int m = s.length();
var cs = new char[m - 1];
for (int i = 0; i < m - 1; ++i) {
cs[i] = (char) (s.charAt(i + 1) - s.charAt(i));
}
var t = String.valueOf(cs);
d.putIfAbsent(t, new ArrayList<>());
d.get(t).add(s);
}
for (var ss : d.values()) {
if (ss.size() == 1) {
return ss.get(0);
}
}
return "";
}
}
// Accepted solution for LeetCode #2451: Odd String Difference
func oddString(words []string) string {
d := map[string][]string{}
for _, s := range words {
m := len(s)
cs := make([]byte, m-1)
for i := 0; i < m-1; i++ {
cs[i] = s[i+1] - s[i]
}
t := string(cs)
d[t] = append(d[t], s)
}
for _, ss := range d {
if len(ss) == 1 {
return ss[0]
}
}
return ""
}
# Accepted solution for LeetCode #2451: Odd String Difference
class Solution:
def oddString(self, words: List[str]) -> str:
d = defaultdict(list)
for s in words:
t = tuple(ord(b) - ord(a) for a, b in pairwise(s))
d[t].append(s)
return next(ss[0] for ss in d.values() if len(ss) == 1)
// Accepted solution for LeetCode #2451: Odd String Difference
use std::collections::HashMap;
impl Solution {
pub fn odd_string(words: Vec<String>) -> String {
let n = words[0].len();
let mut map: HashMap<String, (bool, usize)> = HashMap::new();
for (i, word) in words.iter().enumerate() {
let mut k = String::new();
for j in 1..n {
k.push_str(&(word.as_bytes()[j] - word.as_bytes()[j - 1]).to_string());
k.push(',');
}
let new_is_only = !map.contains_key(&k);
map.insert(k, (new_is_only, i));
}
for (is_only, i) in map.values() {
if *is_only {
return words[*i].clone();
}
}
String::new()
}
}
// Accepted solution for LeetCode #2451: Odd String Difference
function oddString(words: string[]): string {
const d: Map<string, string[]> = new Map();
for (const s of words) {
const cs: number[] = [];
for (let i = 0; i < s.length - 1; ++i) {
cs.push(s[i + 1].charCodeAt(0) - s[i].charCodeAt(0));
}
const t = cs.join(',');
if (!d.has(t)) {
d.set(t, []);
}
d.get(t)!.push(s);
}
for (const [_, ss] of d) {
if (ss.length === 1) {
return ss[0];
}
}
return '';
}
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.