How To Solve The Two Strings Code Challenge
How To Solve HackerRank’s Two Strings Code Challenge With JavaScript
Problem
Let’s break down the challenge into requirements:
- Link to challenge: HackerRank’s Two Strings Code Challenge
- You will receive two strings, s1 and s2
- s1 and s2 can be a string length of 1 to 100000
- If s1 and s2 have common letters in them return “YES”
- If s1 and s2 do NOT have common letters in them return “NO”
// Example 1
// s1 = hello
// s2 = world// Result, (there is "l" and "o" that are common)
"YES"// Example 2
// s1 = hi
// s2 = world
// Result, (there are no common letters)
"NO"
Goal
Write a function or functions that returns “YES” or “NO” if there are common letters between the two strings or not respectively.
Covering Our Bases
Making sure we meet the requirements:
function twoStrings(s1, s2) {
let result = "NO";
const s1len = s1.length;
const s2len = s2.length; if (s1len >= 1
&& s1len <= 100000
&& s2len >= 1
&& s2len <= 100000) {
// continue with code
} return result;
}
Understanding The Problem
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.
Converting To Array
function twoStrings(s1, s2) {
let result = "NO";
const s1len = s1.length;
const s2len = s2.length; if (s1len >= 1
&& s1len <= 100000
&& s2len >= 1
&& s2len <= 100000) {
result = s1.split('');
// hello > ['h', 'e', 'l', 'l', 'o']
} return result;
}
Traversing The Array With Filtering
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.
function twoStrings(s1, s2) {
let result = "NO";
const s1len = s1.length;
const s2len = s2.length; if (s1len >= 1
&& s1len <= 100000
&& s2len >= 1
&& s2len <= 100000) {
result = s1.split('')
.filter((el, key) => s2.indexOf(el) > -1);
// s2 = "world"
// s1 = "hello" > ['h', 'e', 'l', 'l', 'o']
// result = ["l", "l", "o"]
} return result;
}
Getting The Result
Lastly we need to return the right message if the length of the array is great than zero.
function twoStrings(s1, s2) {
let result = "NO";
const s1len = s1.length;
const s2len = s2.length; if (s1len >= 1
&& s1len <= 100000
&& s2len >= 1
&& s2len <= 100000) {
result = (s1.split('')
.filter((el, key) => s2.indexOf(el) > -1)
.length > 0) ? "YES" : "NO";
// s2 = "world"
// s1 = "hello" > ['h', 'e', 'l', 'l', 'o']
// result = ["l", "l", "o"]
// result = "YES"
} return result;
}
Solution
The full solution without the comments:
function twoStrings(s1, s2) {
let result = "NO";
const s1len = s1.length;
const s2len = s2.length; if (s1len >= 1
&& s1len <= 100000
&& s2len >= 1
&& s2len <= 100000) {
result = (s1.split('')
.filter((el, key) => s2.indexOf(el) > -1)
.length > 0) ? "YES" : "NO";
} return result;
}
Test Cases
Now let’s validate the code:
// "hello", "world", Expected "YES"
// "hi", "world", Expected "NO"
// "wouldyoulikefries", "abcabcabcabcabcabc", Expected "NO"
// "hackerrankcommunity", "cdecdecdecde", Expected "YES"twoStrings("hello", "world"); // "YES" ✅
twoStrings("hi", "world"); // "NO" ✅
twoStrings("wouldyoulikefries", "abcabcabcabcabcabc"); // "NO" ✅
twoStrings("hackerrankcommunity", "cdecdecdecde"); // "YES" ✅
Any Feedback?
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.