Skip to content

Commit 3726038

Browse files
add 14078
1 parent 1f687bb commit 3726038

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1408|[String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1408.java) | |Easy|String|
1112
|1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1403.java) | |Easy|Greedy, Sort|
1213
|1401|[Circle and Rectangle Overlapping](https://leetcode.com/problems/circle-and-rectangle-overlapping/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1401.java) | |Medium|Geometry|
1314
|1400|[Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1400.java) | |Medium|Greedy|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
/**
9+
* 1408. String Matching in an Array
10+
*
11+
* Given an array of string words. Return all strings in words which is substring of another word in any order.
12+
* String words[i] is substring of words[j], if can be obtained removing some characters to left and/or right side of words[j].
13+
*
14+
* Example 1:
15+
* Input: words = ["mass","as","hero","superhero"]
16+
* Output: ["as","hero"]
17+
* Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
18+
* ["hero","as"] is also a valid answer.
19+
*
20+
* Example 2:
21+
* Input: words = ["leetcode","et","code"]
22+
* Output: ["et","code"]
23+
* Explanation: "et", "code" are substring of "leetcode".
24+
*
25+
* Example 3:
26+
* Input: words = ["blue","green","bu"]
27+
* Output: []
28+
*
29+
* Constraints:
30+
* 1 <= words.length <= 100
31+
* 1 <= words[i].length <= 30
32+
* words[i] contains only lowercase English letters.
33+
* It's guaranteed that words[i] will be unique.
34+
* */
35+
public class _1408 {
36+
public static class Solution1 {
37+
public List<String> stringMatching(String[] words) {
38+
Set<String> set = new HashSet<>();
39+
for (String word : words) {
40+
for (int i = 0; i < words.length; i++) {
41+
if (!word.equals(words[i]) && word.length() < words[i].length()) {
42+
if (words[i].indexOf(word) != -1) {
43+
set.add(word);
44+
}
45+
}
46+
}
47+
}
48+
List<String> result = new ArrayList<>();
49+
for (String s : set) {
50+
result.add(s);
51+
}
52+
return result;
53+
}
54+
}
55+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1408;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1408Test {
13+
private static _1408.Solution1 solution1;
14+
private static String[] words;
15+
private static List<String> expected;
16+
private static List<String> actual;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _1408.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
words = new String[]{"mass", "as", "hero", "superhero"};
26+
expected = Arrays.asList("as", "hero");
27+
actual = solution1.stringMatching(words);
28+
assertEquals(expected.containsAll(actual), actual.containsAll(expected));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy