Skip to content

Commit abe28ad

Browse files
authored
Merge pull request #219 from sir-gon/feature/two-strings
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: T…
2 parents 9050ac1 + bc36665 commit abe28ad

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* TwoStrings.
8+
*
9+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md]]
10+
* @link Solution notes [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings-solution-notes.md]]
11+
*/
12+
public class TwoStrings {
13+
private TwoStrings() {}
14+
15+
private static final String YES = "YES";
16+
private static final String NO = "NO";
17+
18+
/**
19+
* twoStringsCompute.
20+
*/
21+
public static boolean twoStringsCompute(String s1, String s2) {
22+
23+
Set<String> dictionary = new HashSet<>();
24+
25+
for (char occurrence : s1.toCharArray()) {
26+
dictionary.add("%c".formatted(occurrence));
27+
}
28+
29+
for (char occurrence : s2.toCharArray()) {
30+
if (dictionary.contains("%c".formatted(occurrence))) {
31+
return true;
32+
}
33+
}
34+
35+
return false;
36+
}
37+
38+
public static String twoStrings(String s1, String s2) {
39+
return twoStringsCompute(s1, s2) ? YES : NO;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
/**
14+
* TwoStringsTest.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class TwoStringsTest {
18+
public static class TwoStringsTestCase {
19+
public String title;
20+
public String s1;
21+
public String s2;
22+
public String expected;
23+
}
24+
25+
private List<TwoStringsTestCase> testCases;
26+
27+
@BeforeAll
28+
public void setup() throws IOException {
29+
String path = String.join("/",
30+
"hackerrank",
31+
"interview_preparation_kit",
32+
"dictionaries_and_hashmaps",
33+
"two_strings.testcases.json");
34+
35+
this.testCases = JsonLoader.loadJson(path, TwoStringsTestCase.class);
36+
}
37+
38+
@Test void testTwoStrings() {
39+
for (TwoStringsTestCase test : testCases) {
40+
String solutionFound = TwoStrings.twoStrings(test.s1, test.s2);
41+
42+
assertEquals(test.expected, solutionFound,
43+
String.format("%s(%s, %s) answer must be: %s",
44+
"CrushOptimized.arrayManipulation",
45+
test.s1,
46+
test.s2,
47+
test.expected
48+
)
49+
);
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "Example 1",
4+
"s1": "and",
5+
"s2": "art",
6+
"expected": "YES"
7+
},
8+
{
9+
"title": "Example 2",
10+
"s1": "be",
11+
"s2": "cat",
12+
"expected": "NO"
13+
},
14+
{
15+
"title": "Sample Test Case 0",
16+
"s1": "hello",
17+
"s2": "world",
18+
"expected": "YES"
19+
}
20+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# [Two Strings](https://www.hackerrank.com/challenges/two-strings)
2+
3+
- Difficulty: `#easy`
4+
- Category: `#ProblemSolvingBasic` `#dictionaries` `#hashmaps`
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.
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` `#dictionaries` `#hashmaps`
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)