Skip to content

Commit d5c5836

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Hash Tables: Two Strings. Solved ✓.
1 parent f7608d0 commit d5c5836

File tree

3 files changed

+161
-0
lines changed
  • algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps
  • algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps
  • docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps

3 files changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class TwoStringsTest
5+
{
6+
public class TwoStringsTestCase
7+
{
8+
public string title = "";
9+
public string s1 = "";
10+
public string s2 = "";
11+
public string expected = "Yes";
12+
}
13+
14+
private static readonly TwoStringsTestCase[] tests = [
15+
new()
16+
{
17+
title = "Example 1",
18+
s1 = "and",
19+
s2 = "art",
20+
expected = "Yes"
21+
},
22+
new()
23+
{
24+
title = "Example 2",
25+
s1 = "be",
26+
s2 = "cat",
27+
expected = "No"
28+
},
29+
new()
30+
{
31+
title = "Sample Test Case 0",
32+
s1 = "hello",
33+
s2 = "world",
34+
expected = "Yes"
35+
},
36+
];
37+
38+
[TestMethod]
39+
public void testTwoStrings()
40+
{
41+
string result;
42+
43+
foreach (TwoStringsTestCase test in tests)
44+
{
45+
result = TwoStrings.twoStrings(test.s1, test.s2);
46+
Assert.AreEqual(test.expected, result);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class TwoStrings
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected TwoStrings() { }
11+
12+
private static readonly string __YES__ = "Yes";
13+
private static readonly string __NO__ = "No";
14+
private static readonly char __EMPTY_CHAR__ = '\0';
15+
16+
public static bool twoStringsCompute(string s1, string s2)
17+
{
18+
char occurrence = s1.FirstOrDefault(c => s2.Contains(c), __EMPTY_CHAR__);
19+
20+
if (occurrence != __EMPTY_CHAR__)
21+
{
22+
return true;
23+
}
24+
25+
return false;
26+
}
27+
28+
public static string twoStrings(string s1, string s2)
29+
{
30+
return twoStringsCompute(s1, s2) ? __YES__ : __NO__;
31+
}
32+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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)

0 commit comments

Comments
 (0)