You are given an array of strings words
. Your task is to find all the strings in the array that are a substring of another string in the same array. Return these strings in any order.
Input:
words = ["mass", "as", "hero", "superhero"]
Output:
["as", "hero"]
Explanation:
["hero", "as"]
is also correct.Input:
words = ["leetcode", "et", "code"]
Output:
["et", "code"]
Explanation:
Input:
words = ["blue", "green", "bu"]
Output:
[]
Explanation:
No string in the array is a substring of another string.
The output is an array containing strings that are substrings of other strings in the input array.
https://leetcode.com/problems/string-matching-in-an-array/?envType=daily-question&envId=2025-01-06
Loading component...
Loading component...
Main Function is not defined.
INPUT: words = ["mass", "as", "hero", "superhero"]
OUTPUT: ["as", "hero"]
public static List<String> stringMatching(String[] words) {
List<String> result = new ArrayList<>();
Arrays.sort(words, (a,b) -> Integer.compare(a.length(), b.length() ) );
for( int i = o; i < words.length; i++) {
for ( int j = i+1; j < words.length; j++) {
if ( words[j].contains(words[i])) {
result.add( words[i] );
break;
}//If End
}//Loop End
}//Loop End
return result;
}//function end
Utility Function is not required.