5 回答
TA贡献1825条经验 获得超4个赞
由于只有 26 个字母,而 an 中有 32 位int,因此 anint足以容纳有关单词中出现的字母的所有信息:
static int getFingerprint(String s)
{
int result=0;
for (int i = s.length()-1; i>=0; --i) {
char c = s.charAt(i);
if (c>='a' && c<='z')
result |= 1<<(int)(c-'a');
else if (c>='A' && c<='Z')
result |= 1<<(int)(c-'A');
}
return result;
}
public static void main(String[] args) {
String[] arr = new String[]{"velo", "low", "vole", "lovee", "volvell", "lowly", "lower", "lover", "levo", "loved", "love",
"lovee", "lowe", "lowes", "lovey", "lowan", "lowa", "evolve", "loves", "volvelle", "lowed", "love"};
String s = "love";
int fingerprint = getFingerprint(s);
int matches = 0;
for (String item : arr) {
if (getFingerprint(item)==fingerprint)
++matches;
}
System.out.println(matches);
}
TA贡献1811条经验 获得超6个赞
我建议简化正则表达式,不需要前瞻,简单的“^[love]*$”就可以了。
private static String buildPattern(String s) {
String pattern = "^[";
for (int i = 0; i < s.length(); i++) {
pattern += s.charAt(i);
}
pattern += "]*$";
return pattern;
}
TA贡献1836条经验 获得超4个赞
数 10 应该成功!
String[] arr = new String[] { "velo", "low", "vole", "lovee",
"volvell", "lowly", "lower", "lover", "levo", "loved", "love",
"lovee", "lowe", "lowes", "lovey", "lowan", "lowa", "evolve",
"loves", "volvelle", "lowed", "love" };
String s = "love";
Predicate<Character> p = x -> s.indexOf(x) > -1 ? true : false;
List<String> asList = Arrays.asList(arr);
asList.stream().forEach(x -> {
List<Character> chars = new ArrayList<>();
for (int i = 0; i < x.length(); i++) {
chars.add(x.charAt(i));
}
boolean anyMatch = chars.stream().allMatch(p);
if (anyMatch)
count++;
});
System.out.println(count);
TA贡献1848条经验 获得超10个赞
public static void main(String[] args) {
String[] arr = new String[]{"velo", "low", "vole", "lovee", "volvell", "lowly", "lower", "lover", "levo", "loved", "love",
"lovee", "lowe", "lowes", "lovey", "lowan", "lowa", "evolve", "loves", "volvelle", "lowed", "love"};
String s = "love";
Set<Character> searchWordCharacters = getDistinctCharacters(s);
long result = Stream.of(arr)
.map(Scratch::getDistinctCharacters)
.filter(wordCharacters -> wordCharacters.size() == searchWordCharacters.size())
.filter(wordCharacters -> wordCharacters.containsAll(searchWordCharacters))
.peek(System.out::println)
.count();
System.out.println(result);
}
private static Set<Character> getDistinctCharacters(String word) {
return word.chars()
.mapToObj(i -> (char) i)
.collect(Collectors.toSet());
}
结果:10
TA贡献1799条经验 获得超9个赞
我只计算了 10 个应该成功的,无论是在我的实施中还是在我手动检查时。
很简单,就是比较每个单词中的字母集合是否相等
public static void main(String... args)
{
String word = "love";
List<String> strs = Arrays.asList(
"velo", "low", "vole", "lovee", "volvell", "lowly", "lower", "lover", "levo", "loved", "love",
"lovee", "lowe", "lowes", "lovey", "lowan", "lowa", "evolve", "loves", "volvelle", "lowed", "love"
);
System.out.println(
strs.stream()
.filter(str -> chars(word).equals(chars(str)))
.count()
);
}
private static Set<Character> chars(String word)
{
return word.chars()
.mapToObj(ch -> (char) ch)
.collect(Collectors.toSet());
}
添加回答
举报