Skip to content

Commit 335d50b

Browse files
authored
Merge pull request #2272 from agarwalhimanshugaya/dsa4
add quesno 1408
2 parents 52dfc43 + 0252f1f commit 335d50b

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
id: string-matching-in-an-array
3+
title: String Matching in an Array
4+
sidebar_label: 1408 - String Matching in an Array
5+
tags:
6+
- Array
7+
- String
8+
- String Matching
9+
description: "This is a solution to the String Matching in an Array problem on LeetCode."
10+
---
11+
12+
## Problem Description
13+
14+
Given an array of string `words`, return all strings in words that is a substring of another word. You can return the answer in any order.
15+
16+
A substring is a contiguous sequence of characters within a string
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
```
23+
Input: words = ["mass","as","hero","superhero"]
24+
Output: ["as","hero"]
25+
Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
26+
["hero","as"] is also a valid answer.
27+
```
28+
**Example 2:**
29+
```
30+
Input: words = ["leetcode","et","code"]
31+
Output: ["et","code"]
32+
Explanation: "et", "code" are substring of "leetcode".
33+
34+
```
35+
### Constraints
36+
37+
- `1 <= words.length <= 100`
38+
- `1 <= words[i].length <= 30`
39+
40+
41+
## Solution for String Matching in an Array
42+
43+
## Code in Different Languages
44+
45+
<Tabs>
46+
<TabItem value="cpp" label="C++">
47+
<SolutionAuthor name="@agarwalhimanshugaya"/>
48+
49+
```cpp
50+
class Solution {
51+
public:
52+
vector<string> stringMatching(vector<string>& words) {
53+
vector<string> ans;
54+
for(auto i:words)
55+
{
56+
for(auto j: words)
57+
{
58+
if(i==j) continue;
59+
if(j.find(i)!=-1)
60+
{
61+
ans.push_back(i);
62+
break;
63+
}
64+
}
65+
}
66+
return ans;
67+
}
68+
};
69+
```
70+
</TabItem>
71+
<TabItem value="java" label="Java">
72+
<SolutionAuthor name="@agarwalhimanshugaya"/>
73+
```java
74+
class Solution {
75+
public List<String> stringMatching(String[] words) {
76+
String str = String.join(" ", words);
77+
List<String> res = new ArrayList<>();
78+
for(int i = 0; i < words.length; i++){
79+
if(str.indexOf(words[i]) != str.lastIndexOf(words[i])){
80+
res.add(words[i]);
81+
}
82+
}
83+
return res;
84+
}
85+
}
86+
```
87+
88+
</TabItem>
89+
<TabItem value="python" label="Python">
90+
<SolutionAuthor name="@agarwalhimanshugaya"/>
91+
92+
```python
93+
class Solution:
94+
def stringMatching(self, words: List[str]) -> List[str]:
95+
def add(word: str):
96+
node = trie
97+
for c in word:
98+
node = node.setdefault(c, {})
99+
100+
def get(word: str) -> bool:
101+
node = trie
102+
for c in word:
103+
if (node := node.get(c)) is None: return False
104+
return True
105+
106+
words.sort(key=len, reverse=True)
107+
trie, result = {}, []
108+
for word in words:
109+
if get(word): result.append(word)
110+
for i in range(len(word)):
111+
add(word[i:])
112+
return result
113+
```
114+
</TabItem>
115+
</Tabs>
116+
117+
## Complexity Analysis
118+
119+
### Time Complexity: $O(NlogN + N * S^2)$
120+
121+
### Space Complexity: $O(N * S^2)$
122+
123+
## References
124+
125+
- **LeetCode Problem**: [String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/description/)
126+
127+
- **Solution Link**: [String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/solutions/)

0 commit comments

Comments
 (0)