Leetcode 809. Expressive Words
We are given a string S and a list of strings words. We want to determine how many words in the list can be considered “stretchy.” A word is stretchy if it can be made to match S by adding some number of the same character to every group of consecutive characters in S.
Formally, we can achieve a stretchy word by extending each group of consecutive characters of a word W such that each group W_i can be extended to match group S_i in S, under the conditions:
S_i is 3 or more, we can extend W_i to any length equal to or larger than the length of S_i.S_i is exactly 2, W_i has to match S_i exactly (i.e., len(W_i) == len(S_i)).S_i is exactly 1.Return the number of words in words that are stretchy.
S and words in words constrained in any way?split_to_groups to split any string into groups of consecutive characters along with their counts.is_stretchy to compare the groups from S with groups from a word in words using the given stretching rules.words, split S and the current word into groups, check if the word is stretchy using the is_stretchy function and count the number of stretchy words.import java.util.*;
public class ExpressiveWords {
public static int expressiveWords(String S, String[] words) {
int count = 0;
List<Group> sGroups = split_to_groups(S);
for (String word : words) {
List<Group> wGroups = split_to_groups(word);
if (is_stretchy(sGroups, wGroups)) {
count++;
}
}
return count;
}
private static List<Group> split_to_groups(String str) {
List<Group> groups = new ArrayList<>();
int i = 0;
int len = str.length();
while (i < len) {
char c = str.charAt(i);
int start = i;
while (i < len && str.charAt(i) == c) {
i++;
}
groups.add(new Group(c, i - start));
}
return groups;
}
private static boolean is_stretchy(List<Group> sGroups, List<Group> wGroups) {
int sLen = sGroups.size();
int wLen = wGroups.size();
if (sLen != wLen) return false;
for (int i = 0; i < sLen; i++) {
Group sGroup = sGroups.get(i);
Group wGroup = wGroups.get(i);
if (sGroup.character != wGroup.character) return false;
if (sGroup.count < wGroup.count) return false;
if (sGroup.count > wGroup.count && sGroup.count < 3) return false;
}
return true;
}
private static class Group {
char character;
int count;
public Group(char character, int count) {
this.character = character;
this.count = count;
}
}
public static void main(String[] args) {
String S = "heeellooo";
String[] words = {"hello", "hi", "helo"};
System.out.println(expressiveWords(S, words)); // Output: 1
}
}
S and p is the total length of all words in words.The overall time complexity considering all words is O(m * w * k), where w is the number of words.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?