Pangrams

    by nikoo28

    Question: Given a string, identify if it is a pangram.


    Input:
    The quick brown fox jumps over the lazy little dog.
    Output: pangram

    To start with the problem statement we must first understand what is a pangram. A pangram is a string that contains all the characters in the English alphabet at-least once. This means that the string ‘the quick brown fox jumps over the lazy little dog’ will cover each of the letter from ‘a-z’.

    So how to identify if a string is a pangram?
    You need to keep a check on the occurrence of each of the character present in the string. For the simplicity of our case let us assume that the string has all valid characters which are lowercase. JAVA gives you a feature of a HashSet that only allows one entry of a character.

    So our algorithm would be:-

    • Declare an empty hashset.
    • Iterate over the string and insert each character in the hashset.
    • Once the string has been traversed, check the size of the hashset. If it is equal to 26, that means we have covered each of the character in English alphabet.
    • If the size of hashset is less than 26, it means that the string is not a pangram

    Take a look at the source code.

    public class Pangrams {
    
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            s = s.toLowerCase();
    
            // Declare a hashset
            HashSet<Integer> h = new HashSet<>();
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) != ' ') {
    
                    // Add characters in the hashset.
                    int put = (int) s.charAt(i);
                    h.add(put);
                }
            }
            if (h.size() == 26)
                System.out.println("pangram");
            else
                System.out.println("not pangram");
        }
    }
    Code language: Java (java)

    A working implementation of the code can be found here.

    This problem can also be found at HACKERRANK.

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    [HackerRank] – Two Characters

    by nikoo28
    7 minutes read

    A string is said to be valid when it has only distinct characters and none of them repeat simultaneously. For example, if string ‘s two distinct characters are x and y, then valid examples could be xyxyx or yxyxy but not xxyy or xyyx.Question: Given a sample string, we need to determine what is the maximum length of valid string that can be made by deleting any of the characters.Input: beabeefeabOutput: 5 Explanation: Let us try to understand the output for the sample test case.If we delete e and f, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Next lexicographic string permutation

    by nikoo28
    4 minutes read

    Question: Given a string, find the next lexicographic permutation. Input: abcOutput: acb First of all let us try to understand what do you mean by next lexicographic string. To get a hold of the concept try to remember an English dictionary and how words are arranged in it. We start with the letter ‘a’, write all the words and then move to letter ‘b’ all the way to letter ‘z’. This is called as a lexicographic order.This is possible when we have all the letters in English alphabet available to …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Kth largest element in an array (Method 1)

    by nikoo28
    3 minutes read

    Question: Given an unsorted array of integers and a number ‘k’. Return the kth largest element in the array.Input: arr = {3,2,1,5,6,4}, k = 2Output: 5 Given an unsorted array of integers, you need to determine the kth largest element. By taking a first glance at the problem we can be sure of one thing. If the array is already sorted in ascending order, we can simply return the element at arr[size – k]. This would give us the kth largest element. This is because the 2nd largest element is …

    0 FacebookTwitterLinkedinWhatsappEmail
  • ArraysMisc

    Convert a number into a string of words

    by nikoo28
    4 minutes read

    Question: Given an integer N, convert it into a string of words. Input: N = 345 Output: three hundred forty five This is one of the most common problems that we come across in out daily lives. We need to input a number from the user and print it in words. First, we perform a number of checks on the input string. If it is in any way invalid, the result is the empty string (“”). Next, we take note of whether or not the string begins with a ‘-‘. …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscStrings

    Common String Algorithms

    by nikoo28
    7 minutes read

    In this post, I would try to implement the following string-related algorithm questions. I would like to implement these using Java and try to make additional comments which I think that are useful for understanding the implementations of some type of data structures in Java programming language. Non Repeated Characters in String : Return the unique characters in a given letter. Anagram Strings : Given two strings, check if they’re anagrams or not. Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation and …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the first N prime numbers. (Method 3)

    by nikoo28
    3 minutes read

    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 discussed the basic approaches to find the first N prime numbers in these posts Find the first N prime numbers (Method 1) Find the first N prime numbers (Method 2) It is advised that you go through the above posts to get a basic understanding of the approach that we have used to determine the prime numbers. Now lets analyze …

    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