Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Two Strings problem solution

YASH PAL, 31 July 2024

In this HackerRank Two Strings Interview preparation kit problem solution you have Given two strings, to determine if they share a common substring. A substring may be as small as one character.

HackerRank Two Strings interview preparation kit solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the twoStrings function below.
def twoStrings(s1, s2):
    if set(s1) & set(s2):
        return "YES"
    else: 
        return "NO"

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    q = int(input())

    for q_itr in range(q):
        s1 = input()

        s2 = input()

        result = twoStrings(s1, s2)

        fptr.write(result + 'n')

    fptr.close()

Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class TwoStrings {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	void solve() throws IOException {
		int t = nextInt();
		outer: while (t-- > 0) {
			char[] a = nextToken().toCharArray();
			char[] b = nextToken().toCharArray();
			boolean[] has = new boolean[26];
			for (char c : a) {
				has[c - 'a'] = true;
			}
			for (char c : b) {
				if (has[c - 'a']) {
					out.println("YES");
					continue outer;
				}
			}
			out.println("NO");
		}
	}

	TwoStrings() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new TwoStrings();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}

Problem solution in C++ programming.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a; cin >> a;
    for (int g=0;g<a; g++)
    {
        string b,c; cin >> b >> c; map <char,int> k; 
        for (int y=0;y<b.length(); y++) k[b[y]]=1; int counter=0; 
        for (int y=0;y<c.length(); y++) 
        {
            if (k[c[y]]) counter=1; 
        }
        if (counter) cout << "YES" << 'n';
        else cout << "NO" << 'n'; 
    }return 0; 
}

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t;
    scanf("%d", &t);
    int i, j;
    char letter[26];
    char buf[100001];
    for (i=0; i<t; i++) {
        memset(letter, 0, 26);
        scanf("%s", buf);
        for (j=0; buf[j]!=''; j++) {
            letter[buf[j]-'a'] = 1;
        }
        scanf("%s", buf);
        for (j=0; buf[j]!=''; j++) {
            if (letter[buf[j]-'a']) {
                printf("YESn");
                break;
            }
        }
        if (buf[j] == '') {
            printf("NOn");
        }
    }
    return 0;
}

Problem solution in JavaScript programming.

function containsCommonSubstring(a,b) {
    // Since a one character common substring is still a substring, we can just check for
    // a character in common.  A map should be easy way to do that.
    var map = {};
    for (var i = 0; i < a.length; i++) {
        // We could count it, but just having an entry should be sufficient.  Seems like a boolean.
        map[a[i]] = true;
    }
    for (var i = 0; i < b.length; i++) {
        if (map[b[i]]) return true;
    }
    return false;
}

function processData(input) {
    var lines = input.split("n");
    var T = lines[0];
    for (var i = 0; i < T; i++) {
        var a = lines[2*i+1];
        var b = lines[2*i+2];
        if (containsCommonSubstring(a,b)) {
            process.stdout.write("YESn");
        } else {
            process.stdout.write("NOn");
        }        
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

coding problems solutions interview prepration kit

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes