java中2个字符串的判断
publicbooleanphraseAnagramCheck(Strings1,Strings2){}用递归的方法判断S1和S2有相同的字母。例如phraseAnagr...
public boolean phraseAnagramCheck(String s1, String s2){}
用递归的方法判断 S1和S2有相同的字母。
例如
phraseAnagramCheck("John Kerry", "horny jerk")); //true
phraseAnagramCheck("something", "thingsome")); //true
.phraseAnagramCheck("she loves me", "she loves me not")); //false
phraseAnagramCheck("abcdefg", "aabbccddeeffgg")); //false
phraseAnagramCheck("unempty", "")); //false
谢谢各位 展开
用递归的方法判断 S1和S2有相同的字母。
例如
phraseAnagramCheck("John Kerry", "horny jerk")); //true
phraseAnagramCheck("something", "thingsome")); //true
.phraseAnagramCheck("she loves me", "she loves me not")); //false
phraseAnagramCheck("abcdefg", "aabbccddeeffgg")); //false
phraseAnagramCheck("unempty", "")); //false
谢谢各位 展开
4个回答
展开全部
下面这段代码能够实现你要求的功能:
public class Test {
public static void main(String[] args) {
boolean flag = phraseAnagramCheck("John Kerry", "horny jerk"); //true
System.out.println(flag);
flag = phraseAnagramCheck("something", "thingsome"); //true
System.out.println(flag);
flag = phraseAnagramCheck("she loves me", "she loves me not"); //false
System.out.println(flag);
flag = phraseAnagramCheck("abcdefg", "aabbccddeeffgg"); //false
System.out.println(flag);
flag = phraseAnagramCheck("unempty", ""); //false
System.out.println(flag);
}
public static boolean phraseAnagramCheck(String s1, String s2) {
int count = 0;
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
s1 = s1.replaceAll(" ","");
s2 = s2.replaceAll(" ","");
if(count == s1.length() && count == s2.length()) {
return true;
}
for(int i=0; i<s1.length(); i++) {
for(int j=0; j<s2.length(); j++) {
if(s1.charAt(i) == s2.charAt(j)) {
String replace1 = "" + s1.charAt(i);
String replace2 = "" + s2.charAt(j);
count++;
s1 = s1.replaceFirst(replace1, "");
s2 = s2.replaceFirst(replace2, "");
return phraseAnagramCheck(s1, s2);
}
}
}
return false;
}
}
假如你对上面的方法不满意,网上还有很多其他的方法,比如我找到的:
import java.util.Arrays;
/**
* * Java program - String Anagram Example. * This program checks if two Strings
* are anagrams or not * * @author Javin Paul
*/
public class AnagramCheck {
/*
* * One way to find if two Strings are anagram in Java. This method *
* assumes both arguments are not null and in lowercase. * * @return true,
* if both String are anagram
*/
public static boolean isAnagram(String word, String anagram) {
if (word.length() != anagram.length()) {
return false;
}
char[] chars = word.toCharArray();
for (char c : chars) {
int index = anagram.indexOf(c);
if (index != -1) {
anagram = anagram.substring(0, index)
+ anagram.substring(index + 1, anagram.length());
} else {
return false;
}
}
return anagram.isEmpty();
} /*
* * Another way to check if two Strings are anagram or not in Java * This
* method assumes that both word and anagram are not null and lowercase *
*
* @return true, if both Strings are anagram.
*/
public static boolean iAnagram(String word, String anagram) {
char[] charFromWord = word.toCharArray();
char[] charFromAnagram = anagram.toCharArray();
Arrays.sort(charFromWord);
Arrays.sort(charFromAnagram);
return Arrays.equals(charFromWord, charFromAnagram);
}
public static boolean checkAnagram(String first, String second) {
char[] characters = first.toCharArray();
StringBuilder sbSecond = new StringBuilder(second);
for (char ch : characters) {
int index = sbSecond.indexOf("" + ch);
if (index != -1) {
sbSecond.deleteCharAt(index);
} else {
return false;
}
}
return sbSecond.length() == 0 ? true : false;
}
public static void main(String[] args) {
System.out.println(AnagramCheck.isAnagram("word", "wrdo"));
System.out.println(AnagramCheck.isAnagram("mary", "army"));
System.out.println(AnagramCheck.isAnagram("stop", "tops"));
System.out.println(AnagramCheck.isAnagram("boat", "btoa"));
System.out.println(AnagramCheck.isAnagram("pure", "in"));
System.out.println(AnagramCheck.isAnagram("fill", "fil"));
System.out.println(AnagramCheck.isAnagram("b", "bbb"));
System.out.println(AnagramCheck.isAnagram("ccc", "ccccccc"));
System.out.println(AnagramCheck.isAnagram("a", "a"));
System.out.println(AnagramCheck.isAnagram("sleep", "slep"));
System.out.println();
System.out.println(AnagramCheck.iAnagram("word", "wrdo"));
System.out.println(AnagramCheck.iAnagram("boat", "btoa"));
System.out.println(AnagramCheck.iAnagram("pure", "in"));
System.out.println(AnagramCheck.iAnagram("fill", "fil"));
System.out.println(AnagramCheck.iAnagram("a", "a"));
System.out.println(AnagramCheck.iAnagram("b", "bbb"));
System.out.println(AnagramCheck.iAnagram("ccc", "ccccccc"));
System.out.println(AnagramCheck.iAnagram("sleep", "slep"));
System.out.println();
System.out.println(AnagramCheck.checkAnagram("word", "wrdo"));
System.out.println(AnagramCheck.checkAnagram("b", "bbb"));
System.out.println(AnagramCheck.checkAnagram("ccc", "ccccccc"));
System.out.println(AnagramCheck.checkAnagram("a", "a"));
System.out.println(AnagramCheck.checkAnagram("sleep", "slep"));
System.out.println(AnagramCheck.checkAnagram("boat", "btoa"));
System.out.println(AnagramCheck.checkAnagram("pure", "in"));
System.out.println(AnagramCheck.checkAnagram("fill", "fil"));
}
}
更多追问追答
追答
看不懂什么意思?有其他说明吗?
展开全部
所谓递归 就是函数自己调用自己
那你可以写个函数
假如是
XXX(String s1, String s2);
XXX的作用就是
取S1的第一个字符 然后在S2里删除一个同样的字符
("something", "thingsome" 第一次取就是字符 s )
然后分别取得删除一个字符以后的 S1和S2
(删除以后得到的新s1和 s2就是 omething 和 thingome)
再执行 XXX(s1,s2);
一直递归执行,直到S1或S2为空 停止
比较一下 如果2个都为空 则 true 否则就是false
懒得写代码 写个思路给你
那你可以写个函数
假如是
XXX(String s1, String s2);
XXX的作用就是
取S1的第一个字符 然后在S2里删除一个同样的字符
("something", "thingsome" 第一次取就是字符 s )
然后分别取得删除一个字符以后的 S1和S2
(删除以后得到的新s1和 s2就是 omething 和 thingome)
再执行 XXX(s1,s2);
一直递归执行,直到S1或S2为空 停止
比较一下 如果2个都为空 则 true 否则就是false
懒得写代码 写个思路给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-02-20
展开全部
思路:每次取出子串s1的第一个字符c,看c是否在s2中,然后递归看s1去掉首字符的字串
这个问题递归挺麻烦,你不如直接写嵌套2个for循环得了
public boolean phraseAnagramCheck(String s1, String s2){
char c=s1.charAt(0);
for(int i =0;i<s2.length;i++){
if(c.equals(s2.charAt(i)))
return true;
}
if(s1.lenth>0)
return phraseAnagramCheck(s1.subString(1,s1.lenth),s2);
return false;
}
这个问题递归挺麻烦,你不如直接写嵌套2个for循环得了
public boolean phraseAnagramCheck(String s1, String s2){
char c=s1.charAt(0);
for(int i =0;i<s2.length;i++){
if(c.equals(s2.charAt(i)))
return true;
}
if(s1.lenth>0)
return phraseAnagramCheck(s1.subString(1,s1.lenth),s2);
return false;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
字母???第三个怎么是false???
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询