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 built our palindrome. It consists of as many partnered letters as possible, plus a unique center if possible. This motivates a greedy approach.

    Algorithm:
    We use a HashSet to find the number of partnered characters. Since we are only interested in the length of the longest palindrome possible, we can then have the result.

    public int longestPalindrome(String s) {
    
        if(s==null || s.length()==0)
            return 0;
    
        HashSet<Character> hs = new HashSet<>();
        int count = 0;
    
        for(int i = 0; i < s.length(); i++) {
            if(hs.contains(s.charAt(i))) {
                hs.remove(s.charAt(i));
                count++;
            } else {
                hs.add(s.charAt(i));
            }
        }
        if(!hs.isEmpty())
            return count * 2 + 1;
    
        return count * 2;
    }
    Code language: Java (java)

    A working solution can be found at:- https://ideone.com/8CNFQG

    Time Complexity: O(N), where N is the length of s. We need to count each letter.
    Space Complexity: O(1), the space for our count, as the alphabet size of s is fixed.
    We should also consider that in a bit complexity model, technically we need O(log N) bits to store the count values.

    2 comments
    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
  • Question: Given an integer N, find all the prime factors of the number. Input: N = 45 Output: 3 5 Finding prime factors of any numbers is a very important concept in number theory. One sample problem is available here in which we have to find the largest prime factor. The mathematical concept behind this problem is simple. We start with ‘2’ and keep dividing the original number to get rid of composite factors. Continue this process until the number is no longer divisible by 2. This means that at …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 We have several ways of finding prime numbers. Some of the methods are discussed in the these posts. Method 1 Method 2 Method 3 In this post we will find the first N prime numbers using the Sieve of Eratosthenes. This technique is helpful in scenarios when we have to give immediate results and we need to query the prime numbers …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the sum of even Fibonacci numbers.

    by nikoo28
    3 minutes read

    Question: Find the sum of even fibonacci numbers upto a limit N. Input:100Output: 44 Fibonacci numbers are a miracle of Math and are defined as:- Thus the Fibonacci series can be given as0, 1, 1, 2, 3, 5, 8, 13, 21… Let us try to understand the question. We need to find all the Fibonacci numbers till a limit ‘N’ and then sum only the even numbers in them.Example:N = 10 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8 Sum of even numbers: 2 + 8 = 10 N …

    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