LeetCode 2452: Words Within Two Edits of Dictionary Solution

Master LeetCode problem 2452 (Words Within Two Edits of Dictionary), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2452. Words Within Two Edits of Dictionary

Problem Explanation

Explanation:

To solve this problem, we can iterate through each word in the queries array and compare it with each word in the dictionary array. For each pair of words, we check if they are equal or if they can be made equal with at most two edits. We can achieve this by comparing the characters in both words and keeping track of the differences.

  1. For each word in queries, we iterate through each word in dictionary.
  2. If the words are equal, or if they can be made equal with at most two edits, we add the word from queries to the result list.
  3. We return the list of words from queries that match with some word from dictionary after a maximum of two edits.

Time complexity: O(n * m * k), where n is the number of words in queries, m is the number of words in dictionary, and k is the length of each word. Space complexity: O(n), where n is the number of words in queries.

:

Solution Code

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<String> wordMatches(String[] queries, String[] dictionary) {
        List<String> result = new ArrayList<>();
        
        for (String query : queries) {
            for (String word : dictionary) {
                if (query.equals(word) || areTwoEditsApart(query, word)) {
                    result.add(query);
                    break;
                }
            }
        }
        
        return result;
    }
    
    private boolean areTwoEditsApart(String word1, String word2) {
        int diffCount = 0;
        
        for (int i = 0; i < word1.length(); i++) {
            if (word1.charAt(i) != word2.charAt(i)) {
                diffCount++;
            }
            if (diffCount > 2) {
                return false;
            }
        }
        
        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2452 (Words Within Two Edits of Dictionary)?

This page provides optimized solutions for LeetCode problem 2452 (Words Within Two Edits of Dictionary) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2452 (Words Within Two Edits of Dictionary)?

The time complexity for LeetCode 2452 (Words Within Two Edits of Dictionary) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2452 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2452 in Java, C++, or Python.

Back to LeetCode Solutions