forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1408
No.1408.String Matching in an Array
- Loading branch information
Showing
6 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
solution/1400-1499/1408.String Matching in an Array/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public: | ||
vector<string> stringMatching(vector<string>& words) { | ||
vector<string> ans; | ||
int n = words.size(); | ||
for (int i = 0; i < n; ++i) | ||
{ | ||
for (int j = 0; j < n; ++j) | ||
{ | ||
if (i != j && words[j].find(words[i]) != string::npos) | ||
{ | ||
ans.push_back(words[i]); | ||
break; | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
solution/1400-1499/1408.String Matching in an Array/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
func stringMatching(words []string) []string { | ||
ans := []string{} | ||
for i, w1 := range words { | ||
for j, w2 := range words { | ||
if i != j && strings.Contains(w2, w1) { | ||
ans = append(ans, w1) | ||
break | ||
} | ||
} | ||
} | ||
return ans | ||
} |
15 changes: 15 additions & 0 deletions
15
solution/1400-1499/1408.String Matching in an Array/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public List<String> stringMatching(String[] words) { | ||
List<String> ans = new ArrayList<>(); | ||
int n = words.length; | ||
for (int i = 0; i < n; ++i) { | ||
for (int j = 0; j < n; ++j) { | ||
if (i != j && words[j].contains(words[i])) { | ||
ans.add(words[i]); | ||
break; | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
solution/1400-1499/1408.String Matching in an Array/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Solution: | ||
def stringMatching(self, words: List[str]) -> List[str]: | ||
ans = [] | ||
for i, w1 in enumerate(words): | ||
for j, w2 in enumerate(words): | ||
if i != j and w1 in w2: | ||
ans.append(w1) | ||
break | ||
return ans |