给出两个单词,确定第一个单词或其中的任何一个单词是否出现在第二个单词的连续字符中。例如,tea
在slate
的最后三个字母中显示为anagram,但let
在slate
中不显示为字谜,即使所有let字母出现在石板上。
返回第二个单词中出现的第一个单词的字谜。
Sample Input 1
tea
slate
Sample Output1
ate
Sample Input 2
let
slate
Sample Output2
NONE
我试过下面被击中
public static boolean testAnagram(String originalWord, String containedWord)
{
char [] first = originalWord.toCharArray();//Get the first word and store it into a character array.
char [] second = containedWord.toCharArray();//Get the second word and store it into a character array.
int [] index = new int[second.length];//An int array that stores the index of matching letters contained in the original array.
int counter = 0;//Keep count of the index storage position.
//Compare each character of both words.
for(int i = 0; i < second.length; i++)
{
for(int j = 0; j < first.length; j++)
{
if(second[i] == first[j])
{
index[counter] = j; //Store matching letters.
counter++; //Increment the index storage position.
break;//Break for loop.
}
}
}
if(counter < second.length)return false;//If not all letters were found.
//get the distance between the indices which should be no more than one
//less than the contained word length (to qualify as an anagram)
for(int i = 0; i < index.length; i++)
{
for(int j = 0; j = second.length)
{
//stuck here
return ;
}
}
//If all qualifications are met.
return ;
}
答案 0 :(得分:3)
我建议你做以下事情:
检查此子字符串是否是第一个单词的字谜。
String getSubAnagram (String s1, String s2){
for (int i = 0; i = s1.length(); i++) {
if(s1.indexOf(s2.charAt(i)) >-1){
if(isAnagram(s1, s2.substring(i, s1.length()+i)))
return s2.substring(i, s1.length()+i);
}
}
return null;
}
答案 1 :(得分:1)
g
答案 2 :(得分:0)
对较短的单词中的每个下一个字符使用前向测试,从较长单词中的每个字符开始。要防止双重匹配,请删除较长单词副本中的匹配字符。
只要较长字词中的 next 字符不在较短字词中,您就知道继续使用没有用处。
以下是ANSI C,因此您需要将字符串函数调整为Java等价物。 compareForward
函数修改较短的单词以防止双重匹配 - 它用空格覆盖原始字符 - 因此您需要确保它在输入字的副本上运行。 (我不确定Java是否会自动执行此操作。)
这需要进行O(n *(m-n))次比较。 (请注意,更长的字只需要检查,直到更短的字长仍然存在;因此strlen
中for循环中的main
结束条件。)
#include <stdio.h>
#include <string.h>
int compareForward (char *start, char *findThis)
{
char *copyOfFind, *comparePos, *ptr;
int loopCounter;
printf ("is '%c' in '%s' to begin with?\n", *start, findThis);
if (!strchr (findThis, *start))
{
printf ("first character not in shorter word, no use in continuing\n");
return 0;
}
printf (">\n");
/* C needs a copy because you'll modify the original word otherwise */
copyOfFind = strdup (findThis);
comparePos = start;
for (loopCounter=0; loopCounter < strlen(findThis); loopCounter++)
{
printf ("looking for '%c' in '%s'\n", *comparePos, copyOfFind);
ptr = strchr (copyOfFind, *comparePos);
if (!ptr)
{
printf ("not found, no use in continuing\n");
break;
}
*ptr = ' ';
comparePos++;
}
printf ("<\n");
free (copyOfFind);
/* Did we find all characters? */
return loopCounter == strlen(findThis);
}
int main (int argc, char **argv)
{
int loopy, match;
if (argc != 3)
{
printf ("invalid number of arguments\n");
return 0;
}
match = -1;
for (loopy=0; loopy < strlen(argv[2])-strlen(argv[1])+1; loopy++)
{
printf ("start at %s\n", argv[2]+loopy);
if (compareForward (argv[2]+loopy, argv[1]))
{
match = loopy;
break;
}
}
if (match >= 0)
{
printf ("match at pos %d: ", match);
for (loopy=0; loopy<strlen(argv[1]); loopy++)
printf ("%c", argv[2][match+loopy]);
printf ("\n");
} else
printf ("NONE\n");
return 0;
}