日期:2014-05-20  浏览次数:20838 次

公司要求以最短代码解决下面问题,晕死。。。
现有两个字符串,例如:
String str1 = "新能源汽车,整车,我的,傻×";
String str2 = "新能源汽车,整车,天天,谔谔,我的";
问,上面两个字符串有多少个词组相同(以逗号划分)
汗,我的代码10行,老大说太长了。求助。。。

------解决方案--------------------
变态,
你把10行写成长长的一行吧
------解决方案--------------------
你们老大比较闲呢..............
------解决方案--------------------

蛋疼写法
Java code

    public static void main(String[] args) throws UnknownHostException, IOException 
    {
        String str1 = "新能源汽车,整车,我的,傻×";
        String str2 = "新能源汽车,整车,天天,谔谔,我的";
        for(String str:str1.split(","))
        {
            System.out.println(Arrays.asList(str2.split(",")).contains(str)?str:"");
        }
    }

------解决方案--------------------
Java code

public static void main(String[] args) {
        String str1 = "新能源汽车,整车,我的,傻×";
        String str2 = "新能源汽车,整车,天天,谔谔,我的";
        List<String> l1 = new ArrayList<String>(Arrays.asList(str1.split(",")));
        l1.retainAll(new ArrayList<String>(Arrays.asList(str2.split(","))));
        System.out.println(l1);
    }

------解决方案--------------------
纯新手,刚学习了集合,写个试试,楼主不要笑话啊
Java code

import java.util.*;
public class Stastic
{
    public static void main(String[] args)
    {
        String str1 = "新能源汽车,整车,我的,傻×";
        String str2 = "新能源汽车,整车,天天,谔谔,我的";
        int totalCount=str1.split(",").length+str2.split(",").length;
        Set<String> set=new HashSet<String>(Arrays.asList(str1.split(",")));
        Collections.addAll(set,str2.split(","));
        System.out.println("the repeat string number is "+(totalCount-set.size()));
    }

}