Skip to main content

Command Palette

Search for a command to run...

Verifying an Alien Dictionary

Published
2 min read
Verifying an Alien 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.

In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.

LeetCode Problem - 953

class Solution {
    // Map to store the custom order of characters according to the alien language
    Map<Character, Integer> map;

    public boolean isAlienSorted(String[] words, String order) {
        // Initialize the map with the order of characters in the alien language
        map = new HashMap<>();
        for (int i = 0; i < order.length(); i++) {
            map.put(order.charAt(i), i);
        }

        // Compare each word with the next one to ensure they are sorted
        for (int i = 0; i < words.length - 1; i++) {
            // If any pair of words is not sorted, return false
            if (!checkLexicographically(words[i], words[i + 1])) return false;
        }

        // If all words are sorted, return true
        return true;
    }

    // Helper function to compare two words lexicographically according to the alien language order
    public boolean checkLexicographically(String str1, String str2) {
        int lengthStr1 = str1.length();
        int lengthStr2 = str2.length();
        int minLength = Math.min(lengthStr1, lengthStr2);

        // Compare characters of both strings based on their custom order
        for (int i = 0; i < minLength; i++) {
            int char1Order = map.get(str1.charAt(i));
            int char2Order = map.get(str2.charAt(i));

            // If the first differing character in str1 is less, the pair is in order
            if (char1Order < char2Order) {
                return true;
            } 
            // If the first differing character in str1 is greater, the pair is not in order
            else if (char1Order > char2Order) {
                return false;
            }
        }

        // If all characters compared so far are equal, the shorter string should come first
        return lengthStr1 <= lengthStr2;
    }
}

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.