题目
判断两个字符串是否互为变形词
java代码
/**
* @Description: 判断两个字符串是否互为变形词
* @Author: lizhouwei
* @CreateDate: 2018/4/23 20:51
* @Modify by:
* @ModifyDate:
*/
public class Chapter5_1 {
public boolean isDeformation(String str1, String str2) {
if (str1 == null || str2 == null || str1.length() != str2.length()) {
return false;
}
char[] chas1 = str1.toCharArray();
char[] chas2 = str2.toCharArray();
int[] map = new int[256];
for (int i = 0; i < chas1.length; i++) {
map[chas1[i]]++;
}
for (int i = 0; i < chas2.length; i++) {
if (map[chas2[i]]-- == 0) {
return false;
}
}
return true;
}
//测试
public static void main(String[] args) {
Chapter5_1 chapter = new Chapter5_1();
String str1 = "123";
String str2 = "321";
String str3 = "1121";
System.out.println("123 和 321 为变形词:" + chapter.isDeformation(str1, str2));
System.out.println("321 和 1121 为变形词:" + chapter.isDeformation(str1, str3));
}
}
结果
人们总是在经历失望中习惯孤独,又在重燃希望中找到依附。