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.
Move from brute-force thinking to an efficient approach using array strategy.
Given a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"] Output: "apple"
Example 2:
Input: s = "abpcplea", dictionary = ["a","b","c"] Output: "a"
Constraints:
1 <= s.length <= 10001 <= dictionary.length <= 10001 <= dictionary[i].length <= 1000s and dictionary[i] consist of lowercase English letters.Problem summary: Given a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
"abpcplea" ["ale","apple","monkey","plea"]
"abpcplea" ["a","b","c"]
longest-word-in-dictionary)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #524: Longest Word in Dictionary through Deleting
class Solution {
public String findLongestWord(String s, List<String> dictionary) {
String ans = "";
for (String t : dictionary) {
int a = ans.length(), b = t.length();
if (check(t, s) && (a < b || (a == b && t.compareTo(ans) < 0))) {
ans = t;
}
}
return ans;
}
private boolean check(String s, String t) {
int m = s.length(), n = t.length();
int i = 0;
for (int j = 0; i < m && j < n; ++j) {
if (s.charAt(i) == t.charAt(j)) {
++i;
}
}
return i == m;
}
}
// Accepted solution for LeetCode #524: Longest Word in Dictionary through Deleting
func findLongestWord(s string, dictionary []string) string {
ans := ''
check := func(s, t string) bool {
m, n := len(s), len(t)
i := 0
for j := 0; i < m && j < n; j++ {
if s[i] == t[j] {
i++
}
}
return i == m
}
for _, t := range dictionary {
a, b := len(ans), len(t)
if check(t, s) && (a < b || (a == b && ans > t)) {
ans = t
}
}
return ans
}
# Accepted solution for LeetCode #524: Longest Word in Dictionary through Deleting
class Solution:
def findLongestWord(self, s: str, dictionary: List[str]) -> str:
def check(s: str, t: str) -> bool:
m, n = len(s), len(t)
i = j = 0
while i < m and j < n:
if s[i] == t[j]:
i += 1
j += 1
return i == m
ans = ""
for t in dictionary:
if check(t, s) and (len(ans) < len(t) or (len(ans) == len(t) and ans > t)):
ans = t
return ans
// Accepted solution for LeetCode #524: Longest Word in Dictionary through Deleting
impl Solution {
pub fn find_longest_word(s: String, dictionary: Vec<String>) -> String {
let mut ans = String::new();
for t in dictionary {
let a = ans.len();
let b = t.len();
if Self::check(&t, &s) && (a < b || (a == b && t < ans)) {
ans = t;
}
}
ans
}
fn check(s: &str, t: &str) -> bool {
let (m, n) = (s.len(), t.len());
let mut i = 0;
let mut j = 0;
let s: Vec<char> = s.chars().collect();
let t: Vec<char> = t.chars().collect();
while i < m && j < n {
if s[i] == t[j] {
i += 1;
}
j += 1;
}
i == m
}
}
// Accepted solution for LeetCode #524: Longest Word in Dictionary through Deleting
function findLongestWord(s: string, dictionary: string[]): string {
const check = (s: string, t: string): boolean => {
const [m, n] = [s.length, t.length];
let i = 0;
for (let j = 0; i < m && j < n; ++j) {
if (s[i] === t[j]) {
++i;
}
}
return i === m;
};
let ans: string = '';
for (const t of dictionary) {
const [a, b] = [ans.length, t.length];
if (check(t, s) && (a < b || (a === b && ans > t))) {
ans = t;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.