标签:lse 函数 就是 || rar 存在 str1 rem 难度
给定两个字符串str1和str2,如果str1和str2中出现的字符种类一样且每种字符出现的次数也一样,那么str1和str2互为变形词。请实现函数判断两个字符串是否互为变形词。
例如:
难度:??
boolean isDeformation(String str1,String str2){}
if(str1 == null || str2 == null || str1.length() != str2.length()){
return false;
}
java char[] chars1 = str1.toCharArray(); char[] chars2 = str2.toCharArray();
java int[] ints = new int[256];
for (int i = 0; i < chars1.length; i++) {
ints[chars1[i]]++;
}
for (int i = 0; i < chars2.length; i++) {
if(ints[chars2[i]] == 0){
return false;
}
}
/** 满足条件跳出并返回true */
return true;
import org.junit.Assert;
import org.junit.Test;
/**
* @author lorem
*/
public class StringStringGoTest {
boolean isDeformation(String str1, String str2) {
if (str1 == null || str2 == null || str1.length() != str2.length()) {
return false;
}
char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
int[] ints = new int[256];
for (int i = 0; i < chars1.length; i++) {
ints[chars1[i]]++;
}
for (int i = 0; i < chars2.length; i++) {
if (ints[chars2[i]] == 0) {
return false;
}
}
return true;
}
@Test
public void test(){
String str1 = "asd";
String str2 = "asd";
Assert.assertEquals(true,isDeformation(str1,str2));
}
}
标签:lse 函数 就是 || rar 存在 str1 rem 难度
原文地址:https://www.cnblogs.com/loremwalker/p/9735300.html