File tree Expand file tree Collapse file tree 4 files changed +195
-0
lines changed
docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps
src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps Expand file tree Collapse file tree 4 files changed +195
-0
lines changed Original file line number Diff line number Diff line change
1
+ # [Two Strings](https://www.hackerrank.com/challenges/two-strings)
2
+
3
+ - Difficulty: `#easy`
4
+ - Category: `#ProblemSolvingBasic`
5
+
6
+ ## Clarification
7
+
8
+ The problem asks to find if there is a substring between 2 words,
9
+ it does not require finding a particular one that meets a certain property,
10
+ not counting all the possible ones.
11
+
12
+ With that in mind, simply find the first 1-letter intersection between two word
13
+ to return "YES".
14
+ The worst case is to return "NO" after going through both words letter by letter.
Original file line number Diff line number Diff line change
1
+ # [Two Strings](https://www.hackerrank.com/challenges/two-strings)
2
+
3
+ - Difficulty: `#easy`
4
+ - Category: `#ProblemSolvingBasic`
5
+
6
+ Given two strings, determine if they share a common substring.
7
+ A substring may be as small as one character.
8
+
9
+ ## Example
10
+
11
+ `s1 = 'and'`
12
+ `s1 = 'art'`
13
+
14
+ These share the common substring `a`.
15
+
16
+ `s1 = 'be'`
17
+ `s1 = 'cat'`
18
+
19
+ These do not share a substring.
20
+
21
+ ## Function Description
22
+
23
+ Complete the function twoStrings in the editor below.
24
+
25
+ twoStrings has the following parameter(s):
26
+
27
+ - `string s1`: a string
28
+ - `string s2`: another string
29
+
30
+ ## Returns
31
+
32
+ - `string`: either YES or NO
33
+
34
+ ## Input Format
35
+
36
+ The first line contains a single integer , the number of test cases.
37
+
38
+ The following pairs of lines are as follows:
39
+
40
+ The first line contains string `s1`.
41
+ The second line contains string `s2`.
42
+
43
+ ## Constraints
44
+
45
+ - `s1` and `s2` consist of characters in the range ascii[a-z].
46
+ - $ 1 \leq p \leq 10 $
47
+ - $ 1 \leq |s1|, |s2| \leq 10^5 $
48
+
49
+ ## Output Format
50
+
51
+ For each pair of strings, return `YES` or `NO`.
52
+
53
+ ## Sample Input
54
+
55
+ ```text
56
+ 2
57
+ hello
58
+ world
59
+ hi
60
+ world
61
+ ```
62
+
63
+ ## Sample Output
64
+
65
+ ```text
66
+ YES
67
+ NO
68
+ ```
69
+
70
+ ## Explanation
71
+
72
+ We have pairs to check:
73
+
74
+ 1. `s1 = "hello"`, `s2 = "world"`. The substrings `"o"` and `"l"`
75
+ are common to both strings.
76
+ 2. `a = "hi"`, `b = "world"`. `s1` and `s2` share no common substrings.
77
+
78
+ ## Appendix
79
+
80
+ [Solution notes](two-strings-solution-notes.md)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md]]
3
+ */
4
+
5
+ const __YES__ = 'YES';
6
+ const __NO__ = 'NO';
7
+
8
+ export function twoStringsCompute(s1, s2) {
9
+ for (const char of s1) {
10
+ if (s2.includes(char)) {
11
+ return true;
12
+ }
13
+ }
14
+ return false;
15
+ }
16
+
17
+ export function twoStrings(s1, s2) {
18
+ return twoStringsCompute(s1, s2) ? __YES__ : __NO__;
19
+ }
20
+
21
+ export default { twoStrings };
Original file line number Diff line number Diff line change
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import { logger as console } from '../../../logger.js';
3
+
4
+ import { twoStrings } from './two_strings.js';
5
+
6
+ const TEST_CASES = [
7
+ {
8
+ title: 'Example 1 and 2',
9
+ comparing: [
10
+ {
11
+ s1: 'and',
12
+ s2: 'art',
13
+ expected: 'YES'
14
+ },
15
+ {
16
+ s1: 'be',
17
+ s2: 'cat',
18
+ expected: 'NO'
19
+ }
20
+ ]
21
+ },
22
+ {
23
+ title: 'Sample Test Case 0',
24
+ comparing: [
25
+ {
26
+ s1: 'hello',
27
+ s2: 'world',
28
+ expected: 'YES'
29
+ },
30
+ {
31
+ s1: 'hi',
32
+ s2: 'world',
33
+ expected: 'NO'
34
+ }
35
+ ]
36
+ },
37
+ {
38
+ title: 'Sample Test Case 6',
39
+ comparing: [
40
+ {
41
+ s1: 'wouldyoulikefries',
42
+ s2: 'abcabcabcabcabcabc',
43
+ expected: 'NO'
44
+ },
45
+ {
46
+ s1: 'hackerrankcommunity',
47
+ s2: 'cdecdecdecde',
48
+ expected: 'YES'
49
+ },
50
+ {
51
+ s1: 'jackandjill',
52
+ s2: 'wentupthehill',
53
+ expected: 'YES'
54
+ },
55
+ {
56
+ s1: 'writetoyourparents',
57
+ s2: 'fghmqzldbc',
58
+ expected: 'NO'
59
+ }
60
+ ]
61
+ }
62
+ ];
63
+
64
+ describe('two_strings', () => {
65
+ it('twoStrings test cases', () => {
66
+ expect.assertions(8);
67
+
68
+ TEST_CASES.forEach((testCase) => {
69
+ testCase.comparing.forEach((test) => {
70
+ const answer = twoStrings(test.s1, test.s2);
71
+
72
+ console.debug(
73
+ `checkMagazine(${test.s1}, ${test.s2}) solution found: ${answer}`
74
+ );
75
+
76
+ expect(answer).toStrictEqual(test.expected);
77
+ });
78
+ });
79
+ });
80
+ });
You can’t perform that action at this time.
0 commit comments