-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: T…
…wo Strings. Solved ✓.
- Loading branch information
Gonzalo Diaz
committed
Jul 3, 2024
1 parent
cde09ac
commit f036a93
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...terview_preparation_kit/dictionaries_and_hashmaps/two-strings-solution-notes.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# [Two Strings](https://www.hackerrank.com/challenges/two-strings) | ||
|
||
- Difficulty: `#easy` | ||
- Category: `#ProblemSolvingBasic` | ||
|
||
## Clarification | ||
|
||
The problem asks to find if there is a substring between 2 words, | ||
it does not require finding a particular one that meets a certain property, | ||
not counting all the possible ones. | ||
|
||
With that in mind, simply find the first 1-letter intersection between two word | ||
to return "YES". | ||
The worst case is to return "NO" after going through both words letter by letter. |
80 changes: 80 additions & 0 deletions
80
docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# [Two Strings](https://www.hackerrank.com/challenges/two-strings) | ||
|
||
- Difficulty: `#easy` | ||
- Category: `#ProblemSolvingBasic` | ||
|
||
Given two strings, determine if they share a common substring. | ||
A substring may be as small as one character. | ||
|
||
## Example | ||
|
||
`s1 = 'and'` | ||
`s1 = 'art'` | ||
|
||
These share the common substring `a`. | ||
|
||
`s1 = 'be'` | ||
`s1 = 'cat'` | ||
|
||
These do not share a substring. | ||
|
||
## 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 | ||
|
||
## Input Format | ||
|
||
The first line contains a single integer , the number of test cases. | ||
|
||
The following pairs of lines are as follows: | ||
|
||
The first line contains string `s1`. | ||
The second line contains string `s2`. | ||
|
||
## Constraints | ||
|
||
- `s1` and `s2` consist of characters in the range ascii[a-z]. | ||
- $ 1 \leq p \leq 10 $ | ||
- $ 1 \leq |s1|, |s2| \leq 10^5 $ | ||
|
||
## Output Format | ||
|
||
For each pair of strings, return `YES` or `NO`. | ||
|
||
## Sample Input | ||
|
||
```text | ||
2 | ||
hello | ||
world | ||
hi | ||
world | ||
``` | ||
|
||
## Sample Output | ||
|
||
```text | ||
YES | ||
NO | ||
``` | ||
|
||
## Explanation | ||
|
||
We have pairs to check: | ||
|
||
1. `s1 = "hello"`, `s2 = "world"`. The substrings `"o"` and `"l"` | ||
are common to both strings. | ||
2. `a = "hi"`, `b = "world"`. `s1` and `s2` share no common substrings. | ||
|
||
## Appendix | ||
|
||
[Solution notes](two-strings-solution-notes.md) |
80 changes: 80 additions & 0 deletions
80
src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { logger as console } from '../../../logger'; | ||
|
||
import { twoStrings } from './two_strings'; | ||
|
||
const TEST_CASES = [ | ||
{ | ||
title: 'Example 1 and 2', | ||
comparing: [ | ||
{ | ||
s1: 'and', | ||
s2: 'art', | ||
expected: 'YES' | ||
}, | ||
{ | ||
s1: 'be', | ||
s2: 'cat', | ||
expected: 'NO' | ||
} | ||
] | ||
}, | ||
{ | ||
title: 'Sample Test Case 0', | ||
comparing: [ | ||
{ | ||
s1: 'hello', | ||
s2: 'world', | ||
expected: 'YES' | ||
}, | ||
{ | ||
s1: 'hi', | ||
s2: 'world', | ||
expected: 'NO' | ||
} | ||
] | ||
}, | ||
{ | ||
title: 'Sample Test Case 6', | ||
comparing: [ | ||
{ | ||
s1: 'wouldyoulikefries', | ||
s2: 'abcabcabcabcabcabc', | ||
expected: 'NO' | ||
}, | ||
{ | ||
s1: 'hackerrankcommunity', | ||
s2: 'cdecdecdecde', | ||
expected: 'YES' | ||
}, | ||
{ | ||
s1: 'jackandjill', | ||
s2: 'wentupthehill', | ||
expected: 'YES' | ||
}, | ||
{ | ||
s1: 'writetoyourparents', | ||
s2: 'fghmqzldbc', | ||
expected: 'NO' | ||
} | ||
] | ||
} | ||
]; | ||
|
||
describe('two_strings', () => { | ||
it('twoStrings test cases', () => { | ||
expect.assertions(8); | ||
|
||
TEST_CASES.forEach((testCase) => { | ||
testCase.comparing.forEach((test) => { | ||
const answer = twoStrings(test.s1, test.s2); | ||
|
||
console.debug( | ||
`checkMagazine(${test.s1}, ${test.s2}) solution found: ${answer}` | ||
); | ||
|
||
expect(answer).toStrictEqual(test.expected); | ||
}); | ||
}); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md]] | ||
*/ | ||
|
||
const __YES__ = 'YES'; | ||
const __NO__ = 'NO'; | ||
|
||
export function twoStringsCompute(s1: string, s2: string): boolean { | ||
for (const char of s1) { | ||
if (s2.includes(char)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export function twoStrings(s1: string, s2: string): string { | ||
return twoStringsCompute(s1, s2) ? __YES__ : __NO__; | ||
} | ||
|
||
export default { twoStrings }; |