we will say that two words are "charecter equale" if both of them has the same charecter, for example:
baac
andabac
are charecter equale, I am trying to write a recursive function that gets a string s, a word w and integer k, that checks if there are exactliy k words in the string that they charecter equale to the word, for example: the function should returntrue
for the wordabac
, the stringaabc abdca caba xyz ab
and the numberk=2
.
Ineed help at the recursive part, i.e the function searchMixed
, my idea was first the check if the string contain only on word (base case),
the general case is to call the function searchMixed
without the first word
public class recursion1 {
public static void main(String[] args) {
boolean result=searchMixed("abac","aabc abdca caba xyz ab",2);
System.out.println("result: "+result);
}
public static boolean searchMixed(String word, String s, int k)
{
if(s.indexOf(' ')==-1 && isEquale(word,s) && k==1)
return true;
if(s.indexOf(' ')==-1 && !isEquale(word,s) && k==1)
return false;
int pos=s.indexOf(' ');
System.out.println("index of"+ s.indexOf(' '));
String first_word=first_word=s.substring(0,pos);
if(isEquale(word, first_word))
searchMixed(word, s.substring(pos+1), k-1);
else
searchMixed(word, s.substring(pos+1), k);
return false;
}
.
//this function works fine, the function checks if two words are charecter equale
public static boolean isEquale(String word, String sub_string)
{
if(word.length()!=sub_string.length())
return false;
char[] s33=new char[sub_string.length()];
char[] sww=new char[word.length()];
for(int i=0;i<sub_string.length();i++)
s33[i]=sub_string.charAt(i);
for(int i=0;i<word.length();i++)
sww[i]=word.charAt(i);
for(int i=0;i<word.length();i++)
{
for(int j=0;j<sub_string.length();j++)
{
if(sww[i]==s33[j])
s33[j]='@';
}
}
for(int i=0;i<sub_string.length();i++)
if(s33[i]!='@')
return false;
return true;
}
}
答案 0 :(得分:1)
YourString = YourString.replaceAll("\\s+", " ");
String[] words = YourString.split(" ");
...分开单词。
static int n = 0;
static String keyword = "aabc";
static String[] words = null;
public static void main()
{
n = 0;
// Let's assume you accept 'k' here.
String YourString = "aabc baca hjfg gabac";
words = YourString.split(" ");
rec(words[0]);
if (k <= n)
System.out.println(true);
else
System.out.println(false);
}
static int pos = 0;
public static void rec(String word)
{
boolean flag = true;
word += " ";
if(word.length() != keyword.length() + 1)
{
flag = false;
}
for(int i = 0; i < keyword.length() && flag; i++)
{
for(int j = 0; j < word.length(); j++)
{
if(word.charAt(j) == keyword.charAt(i))
{
word = word.substring(0, j) + word.substring(j+1);
break;
}
}
if(word.equals(" "))
{
n++;
break;
}
}
if(pos + 1 != words.length)
{
rec(words[++pos]);
}
}
现在,让我解释一下:
在递归方法rec(String word)
中,在被检查单词的末尾添加一个空格(以便substring(j+1)
不会超出范围)
如果关键字和选中的单词长度不同,则会停止检查,然后转到“5”。
如果这两个单词的长度相同,那么循环会从单词中删除一个相似的字符(这就是word = word.substring(0, j) + word.substring(j+1);
所做的)。
在循环结束时,如果该单词的剩余部分是空格,则计数器n
增加1并且循环退出。
如果数组中有多于或等于一个字符串,则在数组中检查的字的位置增加1,并且数组中的下一个字传递给rec(String word)
方法