Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Two Strings problem solution

YASH PAL, 31 July 20246 February 2026

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

Function Description

Complete the function twoStrings in the editor below.

twoStrings has the following parameter(s):

  • string s1: a string
  • string s2: another string

Returns

  • string: either YES or NO
HackerRank Two Strings interview preparation kit solution

HackerRank Two Strings problem solution in Python.

#!/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()

Two Strings problem solution in Java.

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 Hackerrank Problems Solutions interview prepration kit HackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes