CamelCase matching

    by nikoo28

    Question: Given a set of queries and a pattern, we need to determine if some of those queries can be made from the given pattern by adding one or more lower case character. Output a boolean array where array[i] says true, if the query is possible, else false.


    Input: queries= {“StudyAlgorithms”, “StudyAlgorithmsTest”, “StanAlluring”, “SimulationAlteration”, “StopStayAlive”}
    pattern = “StAl”

    Output: {true, false, true, true, false}

    Let us try to understand the output.
    The pattern defined to us is St*Al* (if we see it in terms of a regex)

    • StudyAlgorithms can be made as St + “udy” + Al + “gorithms
    • StanAlluring can be made as St + “an” + Al + “luring
    • SimulationAlteration can be made as S + “imula” + t + “ion” + Al + “teration

    Other queries cannot be formed as either they do not follow the pattern or they add another upper case letter.

    ALGORITHM

    – Start for each string in the query array.
    – For each string, match it with the pattern and pass the result.

    The match process should be defined as:-

    – Let ‘i’ be the query pointer.
    – Let ‘j’ be the pattern pointer
    – If char[i] == char[j], we need to advance both the pointers.
    – If char[i] != char[j] and char[i] (query pointer) is a lower case letter, just advance the query pointer. We can keep going as we can add any number of lower case characters.
    – If char[i] != char[j] and char[i] (query pointer) is a upper case letter, then return false. This means that we added a new uppercase letter and it would not match the pattern.

    The code for the above algorithms can be written as:

    public List<Boolean> camelMatch(String[] queries, String pattern) {
        List<Boolean> result = new ArrayList<>();
    
        char[] patternArr = pattern.toCharArray();
    
        // Start for each string in the query array
        for (String query : queries) {
    
            // Match it with the pattern
            boolean isMatch = match(query.toCharArray(), patternArr);
    
            // Pass the result
            res.add(isMatch);
        }
    
        return res;
    }
    
    private boolean match(char[] queryArr, char[] patternArr) {
    
        int j = 0; // pattern pointer
    
        // i is the query pointer
        for (int i = 0; i < queryArr.length; i++) {
    
            // If char[i] == char[j], we need to advance both the pointers.
            if (j < patternArr.length && queryArr[i] == patternArr[j]) { 
                j++; 
            } else if (queryArr[i] >= 'A' && queryArr[i] <= 'Z') {
    
                // If the query character is a uppercase letter, then return false.
                return false;
            }
        }
    
        // Just verify that pattern pointer reaches its length limit
        return j == patternArr.length;
    }
    Code language: Java (java)

    A working solution can be found here. https://ideone.com/Vj89bz

    1 comment
    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Container with maximum water

    by nikoo28
    4 minutes read

    Question: Let us suppose we have a two dimensional plane where the the length of lines determine the height of a container. We need to determine the maximum capacity of water this kind of an arrangement can hold. The heights are represented by an array. Input: [1, 8, 6, 2, 5, 4, 8, 3, 7] Output: 49 Let us try to visualize the problem at hand diagrammatically. Let the array be represented as: If we try to pour water from the top in the entire arrangement you can visualize that …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array, there is a largest element N. Check if that number is at least twice than all the other elements in the array. Return the index if it is, else return -1Input: {3, 6, 1, 0}Output: -1 6 is the largest integer, and for every other number in the array x,6 is more than twice as big as x. The index of value 6 is 1, so we return 1. Let us try to make one more exampleInput: nums = [1, 2, 3, 4] Output: -1Explanation: 4 …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.Input: “abccccdd”Output: 7 In the above example, the longest palindrome in the given string is “dccaccd” whose length is 7. A palindrome consists of letters with equal partners, plus possibly a unique center (without a partner). The letter i from the left has its partner i from the right. For example in ‘abcba’, ‘aa’ and ‘bb’ are partners, and ‘c’ is a unique center. Imagine we …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Output: 0 The easier method to find the cycle has been discussed in this post. This method is based upon the idea of a circular race track. If there is a slow runner and a fast runner, eventually the fast runner will catch up the slow runner from behind. A similar analogy can be applied to a tortoise and a hare and hence the method is also called Tortoise and Hare …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked List

    Find cycle in a linked list. (Method 1)

    by nikoo28
    3 minutes read

    Question: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Output: 0 We are given a linked list which may or may not have a loop. A loop in a linked list means that if we keep doing a next, we will never reach null. Thus, we can be clear of a fact that if we reach null, then the list does not have a loop. So how do we detect if there is a loop in the linked list. One …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Next Greater Element in an array. [NGE]

    by nikoo28
    5 minutes read

    Question: There is an array A[N] of N numbers. You have to compose an array Output[N] such that each element in Output[i] will tell the next greater element to the right in the original array. If there is no greater number to the right, then the output array should contain -1 in that position.Array 1: {4, 6, 5, 4, 3, 4, 9, 8, 1} Output: {6, 9, 9, 9, 4, 9, -1, -1, -1} Let us try to understand the problem statement. We are given an input array that has …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More