Question: Given two strings, determine if they share a common sub-string. If they have a common sub-string, print YES else print NO.


    Input:

    studyalgorithms
    algos
    Output: YES

    This is one of the easy problems but can get a little tricky, if we do not understand it completely. Let us try to understand the test case
    String 1 = “studyalgorithms”
    String 2 = “algos”
    The sub-string “algo” is common between both the sub-strings and therefore the answer is “YES“.

    Let us try to take one more test case.
    String 1 = “hello”
    String 2 = “world”
    The answer would still be “YES“, because “o” and “l” are still strings even though they are single characters.

    Here is an example of a test case where the output would be “NO
    String 1 = “hi”
    String 2 = “alex”
    There is no common sub-string between them.

    Thus here are two concepts involved in solving this challenge:

    • Understanding that a single character is a valid sub-string.
    • Deducing that we only need to know that the two strings have a common sub-string — we don’t need to know what that sub-string is.

    Thus, the key to solving this challenge is determining whether or not the two strings share a common character.

    To do this, we create two sets, ‘a’ and ‘b’, where each set contains the unique characters that appear in the string it’s named after. Because sets don’t store duplicate values, we know that the size of our sets will never exceed the letters of the English alphabet. In addition, the small size of these sets makes finding the intersection very quick.

    If the intersection of the two sets is empty, we print NO on a new line; if the intersection of the two sets is not empty, then we know that strings and share one or more common characters and we print YES on a new line.

    import java.util.HashSet;
    import java.util.Scanner;
    import java.util.Set;
    
    /**
     * Created by studyalgorithms.com
     */
    
    public class TwoStrings {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int n = scan.nextInt();
    
            Set<Character> a;
            Set<Character> b;
    
            for (int i = 0; i < n; i++) {
    
                a = new HashSet<>();
                b = new HashSet<>();
    
                for (char c : scan.next().toCharArray()) {
                    a.add(c);
                }
                for (char c : scan.next().toCharArray()) {
                    b.add(c);
                }
    
                // store the set intersection in set 'a'
                a.retainAll(b);
    
                System.out.println((a.isEmpty()) ? "NO" : "YES");
            }
            scan.close();
        }
    }
    Code language: Java (java)

    A working implementation of the code can be found here.
    This problem is also available on Hackerrank.

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    [Hackerrank] – Multiples of 3 and 5

    by nikoo28
    3 minutes read

    Question: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below ‘N’ Input: N = 100 Output: 2318 The most naive approach to solve this problem will be Iterate over each number till N If the number is divisible by 3 or 5, add it to the sum Print the sum This approach can work well to numbers …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    [Hackerrank] – Palindrome Index

    by nikoo28
    3 minutes read

    Question: Given a string, identify the index of character to be removed to change the string into a palindrome. If the string cannot be converted to palindrome or is already a palindrome just return -1. Input: abckdeedcbaOutput: 3 (0 based indexing) To start off with the problem, we must first understand that there can be two possible ways to make the string a palindrome. If the given string is “bcbc”. If we remove the first ‘b’, the string becomes “cbc” which is a palindrome. If we remove the last ‘c’, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Let us try to simplify the problem as much as we can. We are given with some of the terms. Weights:- According to the problem each character in the English alphabet has been assigned a weight. ‘a’ has been given a weight of 1, ‘b’ has a weight of 2, and so on. The weight of ‘z’ is 26. All the letters are assumed to be lowercase for simplicity. Weight of a string:- This is defined as the sum of all the characters in the string. Example if a string …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Pangrams

    by nikoo28
    2 minutes read

    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 …

    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

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