![JAR search and dependency download from the Maven repository](/logo.png)
g1401_1500.s1408_string_matching_in_an_array.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g1401_1500.s1408_string_matching_in_an_array;
// #Easy #String #String_Matching #2024_12_19_Time_1_ms_(100.00%)_Space_42.7_MB_(5.57%)
import java.util.ArrayList;
import java.util.List;
public class Solution {
public List stringMatching(String[] words) {
List matchedStrings = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
boolean containsSubstring = checkContains(words, i);
if (containsSubstring) {
matchedStrings.add(words[i]);
}
}
return matchedStrings;
}
private boolean checkContains(String[] words, int index) {
for (int j = 0; j < words.length; j++) {
if (index == j) {
continue;
}
if (words[j].contains(words[index])) {
return true;
}
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy