如何在 java 中编写计算 Anagrams 出现次数的算法?
How to write algorithm for Count Occurrences of Anagrams in java?
嗨,我想在 java 中编写字谜算法。我的要求是如果有人给出这样的字符串
Input: "aa aa odg dog gdo"
它的字谜计数应该是 2。谁能帮我解决这个问题?
我尝试了一个解决方案,但它无法正常工作。
public static void main(String[] args) {
String text = "a c b c run urn urn";
String word = "urn";
System.out.print(countAnagrams(text, word));
}
static boolean araAnagram(String s1,
String s2)
{
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
if (Arrays.equals(ch1, ch2))
return true;
else
return false;
}
static int countAnagrams(String text, String word)
{
int N = text.length();
int n = word.length();
int res = 0;
for (int i = 0; i <= N - n; i++) {
String s = text.substring(i, i + n);
if (araAnagram(word, s))
res++;
}
return res;
}
这个程序不符合我的要求
请帮忙
假设有一种方法可以将一个单词转换成一个有序的字符序列,那么使用Stream可以计算出字谜的数量API:
static String anagram(String s) {
char[] arr = s.toCharArray();
Arrays.sort(arr);
return new String(arr);
}
static long countAnagrams(String input) {
if (null == input || input.isEmpty()) {
return 0;
}
return Arrays.stream(input.split("\s+")) // Stream of words
.distinct() // get unique words and
// move them into Map<String, List<String>>, where List contains all anagrams contained in the input string
.collect(Collectors.groupingBy(MyClass::anagram))
.entrySet() //
.stream() // stream of entries
.filter(e -> e.getValue().size() > 1) // filter values with at least two anagrams
.peek(e -> System.out.println(e.getValue())) // debug print of the anagram list
.count(); // and count them
}
测试:
String[] tests = {
"aa aa odg dog gdo",
"cars are very cool so are arcs and my os"
};
Arrays.stream(tests)
.forEach(s -> System.out.printf("'%s' -> anagram count=%d%n", s, countAnagrams(s)));
输出
[odg, dog, gdo]
'aa aa odg dog gdo' -> anagram count=1
[so, os]
[cars, arcs]
'cars are very cool so are arcs and my os' -> anagram count=2
这个可以解决
public static boolean anagrams(String s1, String s2){
if(!(s1.equalsIgnoreCase(s2))){
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
return Arrays.equals(ch1,ch2);
}
return false;
}
public static String CountingAnagrams(String str) {
int res = 0;
String[] splitStr = str.split("\s+");
for(int i = 0 ; i< splitStr.length; i++)
for (int j = i + 1; j < splitStr.length; j++)
if (anagrams(splitStr[i], splitStr[j]))
res++;
return String.valueOf(res-1);
}
嗨,我想在 java 中编写字谜算法。我的要求是如果有人给出这样的字符串
Input: "aa aa odg dog gdo"
它的字谜计数应该是 2。谁能帮我解决这个问题?
我尝试了一个解决方案,但它无法正常工作。
public static void main(String[] args) {
String text = "a c b c run urn urn";
String word = "urn";
System.out.print(countAnagrams(text, word));
}
static boolean araAnagram(String s1,
String s2)
{
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
if (Arrays.equals(ch1, ch2))
return true;
else
return false;
}
static int countAnagrams(String text, String word)
{
int N = text.length();
int n = word.length();
int res = 0;
for (int i = 0; i <= N - n; i++) {
String s = text.substring(i, i + n);
if (araAnagram(word, s))
res++;
}
return res;
}
这个程序不符合我的要求 请帮忙
假设有一种方法可以将一个单词转换成一个有序的字符序列,那么使用Stream可以计算出字谜的数量API:
static String anagram(String s) {
char[] arr = s.toCharArray();
Arrays.sort(arr);
return new String(arr);
}
static long countAnagrams(String input) {
if (null == input || input.isEmpty()) {
return 0;
}
return Arrays.stream(input.split("\s+")) // Stream of words
.distinct() // get unique words and
// move them into Map<String, List<String>>, where List contains all anagrams contained in the input string
.collect(Collectors.groupingBy(MyClass::anagram))
.entrySet() //
.stream() // stream of entries
.filter(e -> e.getValue().size() > 1) // filter values with at least two anagrams
.peek(e -> System.out.println(e.getValue())) // debug print of the anagram list
.count(); // and count them
}
测试:
String[] tests = {
"aa aa odg dog gdo",
"cars are very cool so are arcs and my os"
};
Arrays.stream(tests)
.forEach(s -> System.out.printf("'%s' -> anagram count=%d%n", s, countAnagrams(s)));
输出
[odg, dog, gdo]
'aa aa odg dog gdo' -> anagram count=1
[so, os]
[cars, arcs]
'cars are very cool so are arcs and my os' -> anagram count=2
这个可以解决
public static boolean anagrams(String s1, String s2){
if(!(s1.equalsIgnoreCase(s2))){
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
return Arrays.equals(ch1,ch2);
}
return false;
}
public static String CountingAnagrams(String str) {
int res = 0;
String[] splitStr = str.split("\s+");
for(int i = 0 ; i< splitStr.length; i++)
for (int j = i + 1; j < splitStr.length; j++)
if (anagrams(splitStr[i], splitStr[j]))
res++;
return String.valueOf(res-1);
}