字符串长度最大乘积

字符串长度最大乘积

http://www.nowcoder.com/questionTerminal/480caa5ffd164ac8b71caaa6d0f4e6db

好像只能暴力求解了,数据比较小,用时22ms

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    /**
     * 本来想着这种暴力方法会超时,因为是在没想到好的方法....
     */
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = bf.readLine();
        str = str.substring(1,str.length()-1);//数据处理,去掉[ 和 ]括号
        str = str.replace("\"","");//去掉 ",注意用转移符\"
        String[] dic = str.split(",");
        int max_leng = 0;
        for (int i = 0; i < dic.length; i++) {
            for (int j = i+1; j < dic.length; j++) {
                if (noContain(dic[i], dic[j])) {
                    max_leng = Math.max(max_leng, dic[i].length() * dic[j].length());
                }
            }
        }
        System.out.println(max_leng);
    }

    //判断两个字符是否重复,只能每个字符每个字符的比较了
    private static boolean noContain(String s1, String s2) {
        for (int i = 0; i < s1.length(); i++) {
            if (s2.contains(s1.substring(i, i + 1))) {
                return false;
            }
        }
        return true;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
02-12 18:14
RT,这周五就是情人节了,前女友给我发了消息,我该不该回?
Yoswell:原则上来说让她滚,但是本着工作很累下班想吃瓜的心态,我觉得你可以回一下
点赞 评论 收藏
分享
暮雨轻歌:看起来hr不能接受我菜查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务