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. 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 interview prepration kit