problem solved and The initial Test cases passed by doing the suggestions given by the commenters:adding break; after "YES" and use substring(i,i+1). However, when I submitted the code after suggested corrections, I got "Time limit exceeded" problem. Show
Also, I cannot add return inside the for loop/if statement,because the compiler of course wants a return in the method body but outside the for loop. So, any suggestion?
is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs. Learn More CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance. Learn More
Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying. Learn More Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience. Learn More
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]!='\0'; j++) { letter[buf[j]-'a'] = 1; } scanf("%s", buf); for (j=0; buf[j]!='\0'; j++) { if (letter[buf[j]-'a']) { printf("YES\n"); break; } } if (buf[j] == '\0') { printf("NO\n"); } } 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("YES\n"); } else { process.stdout.write("NO\n"); } } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });Let’s break down the challenge into requirements: Goal Write a function or functions that returns “YES” or “NO” if there are common letters between the two strings or not respectively. Making sure we meet the requirements: We have two string and we need to traverse each letter to see if at least one letter is the same in each strings. A way that we can loop over things is using arrays, but we first need to convert one of strings to an array. Next, we’ll use filter because instead of map, we want to remove any character that isn’t present in the other string. If there are any left, then we know there are some common letters. Lastly we need to return the right message if the length of the array is great than zero. The full solution without the comments: Now let’s validate the code: If you have any tips on how this can be better coded or talk about coding, I would love to talk. If you got value from this, please share it on twitter 🐦 or other social media platforms. Thanks again for reading. 🙏 Please also follow me on twitter: @codingwithmanny and instagram at @codingwithmanny. |