Skip to main content

Command Palette

Search for a command to run...

Word Pattern

Published
2 min read
Word Pattern
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.

Given a pattern and a string s, find if s follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

LeetCode Problem - 290

class Solution {
    // Function to check if a pattern matches a string
    public boolean wordPattern(String pattern, String s) {
        // Split the string into an array of words
        String[] sArray = s.split(" ");
        // If the length of pattern and words array is not equal, return false
        if (pattern.length() != sArray.length) return false;

        // Map to store the mapping of pattern characters to words
        Map<String, String> map1 = new HashMap<>();
        // Iterate through the pattern
        for (int i=0; i<pattern.length(); i++){
            // Get the current character in the pattern
            String currentChar = String.valueOf(pattern.charAt(i));
            // If the current character is not already mapped and the corresponding word is not already used, add the mapping
            if (!map1.containsKey(currentChar) && !map1.containsValue(sArray[i])){
                map1.put(currentChar, sArray[i]);
            }
        }

        // Iterate through the pattern again
        for (int i=0; i<pattern.length(); i++){
            // Get the current character in the pattern
            String currentChar = String.valueOf(pattern.charAt(i));
            // Get the corresponding word in the string and its mapped pattern value
            String currentStringOfs = sArray[i], currentPatternValue = map1.get(currentChar);
            try{
                // If the mapped pattern value does not match the corresponding word, return false
                if (!currentPatternValue.equals(currentStringOfs)){
                    return false;
                }
            } catch (Exception e){
                // If an exception occurs (e.g., null pointer), return false
                return false;
            }
        }
        // If all checks passed, return true
        return true;
    }
}

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.