Skip to main content

Command Palette

Search for a command to run...

Words Within Two Edits of Dictionary

Published
2 min read
Words Within Two Edits of Dictionary
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given two string arrays, queries and dictionary. All words in each array comprise of lowercase English letters and have the same length.

In one edit you can take a word from queries, and change any letter in it to any other letter. Find all words from queries that, after a maximum of two edits, equal some word from dictionary.

Return a list of all words from queries, that match with some word from dictionary after a maximum of two edits. Return the words in the same order they appear in queries.

LeetCode Problem - 2452

class Solution {

    // Method to find words from the queries array that differ by at most two characters from any word in the dictionary
    public List<String> twoEditWords(String[] queries, String[] dictionary) {

        List<String> listAns = new ArrayList<>();  // List to store valid words from queries

        // Iterate through each word in the queries array
        for (String str : queries) {
            // Check if the word can be found in the dictionary with at most two edits
            boolean tempFlag = checkWord(str, dictionary);

            // If the word meets the criteria, add it to the answer list
            if (tempFlag){
                listAns.add(str);
            }
        }

        return listAns;  // Return the list of valid words
    }

    // Helper method to check if the given word can be converted to any word in the dictionary with at most two edits
    public boolean checkWord(String word, String[] dict){
        // Loop through each word in the dictionary
        for (String str : dict) {
            int count = 0;  // Variable to count the number of differing characters

            // Compare the characters of both words one by one
            for (int i = 0; i < str.length(); i++) {
                // Increment the count if characters don't match
                if (str.charAt(i) != word.charAt(i)) {
                    count++;
                    // If the count exceeds 2, break out of the loop
                    if (count > 2) break;
                }
            }

            // If the number of differing characters is less than or equal to 2, return true
            if (count <= 2) return true;
        }

        return false;  // Return false if no matching word is found within two edits
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.