如:String str1 = "aba";
String str2 = "abbbbv";
其中字符a与str2中的a重复2次,b重复4次,则打印“bbbb”
String str2 = "abbbbv";
其中字符a与str2中的a重复2次,b重复4次,则打印“bbbb”
解决方案 »
- 链接不上smtp服务器
- 如何将存入Vector类中的Point类转换回来??
- 请问键盘上的windows键,在程序中应该用什么码来表示?
- 怎么将DOS下的返回结果重定向到一个新的GUI界面
- IP数据包监控程序中出现的问题
- 在一个jlist里add或者remove一条记录怎么实现.资料上和java2s上的例程 都是通过listmodel.addelement...类似的方法实现.但是listmodel下
- 二进制文件和文本文件有什么区别?
- 巨菜的问题,关于frame的关闭.
- Image只能画在Applet里吗?请进来看看这个程序
- 请问那里有VISUAL AGE FOR JAVA 的下载
- ByteArrayInputStream 遇到的问题:类型转换
- 请教类的对象提取session数据的问题
for(int i = 0 ; i < str1; i++){
int num = 0;
string str11 = str1(i,i+1);
for(int j = 0 ; j < str1; j++){
string str22 = str1(j,j+1);
if(str11.equal(str22)){
num ++;
}
numList .add(num);
}
}
然后从numList里面去出来最大值
for(int i = 0 ; i < str1; i++){
int num = 0;
string str11 = str1.subString(i,i+1);
for(int j = 0 ; j < str1; j++){
string str22 = str2.subString(j,j+1);
if(str11.equal(str22)){
num ++;
}
numList .add(num);
}
}
我不是要求出重复数量,而是要求出重复最多的字符串,如果str1中的字符a与str2中的a重复2次是最多的话,就打印“aa”,如b重复4次,是最多的,则打印“bbbb”
我的程序是这样,但我感觉应该还有更简单的:
public static String test(String str1, String str2) {
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i <= str1.length() - 1; i++) {
String ai = String.valueOf(str1.charAt(i));//获得str1的第一个字符
for (int j = 0; j <= str2.length() - 1; j++) {
String aj = String.valueOf(str2.charAt(j));//获得str2的第一个字符
if (ai.equals(aj)) {//如果两个字符相等,则判断是否已经比较过这个字符
if (map.containsKey(ai)) {
map.put(ai, map.get(ai) + aj);
} else {
map.put(ai, ai);
}
}
}
}
Set<String> set1 = map.keySet();
String max = null;
for (String key : set1) {
String temp = map.get(key);
System.out.println("key == "+key+" value == "+temp);
if(max != null && max.length() < temp.length()){
max = temp;
}else{
max = map.get(key);
}
}
return max;
}
Set<String> set1 = map.keySet();
String max = null;
for (String key : set1) {
String temp = map.get(key);
System.out.println("key == "+key+" value == "+temp);
if(max != null && max.length() < temp.length()){
max = temp;
}else if(max == null){//此处增加一个判断
max = map.get(key);
}
}