首页 > 试题广场 >

用最快方法判断所有String2的字母在String1里是否

[问答题]
用最快方法判断所有String2的字母在String1里是否存在,如:string2="abx",string1="abcdef",ab在string1中,x不在。
建一个26个元素的bool数组……检索时间复杂度O(1)
……前边几位都学傻了
发表于 2015-06-15 14:44:46 回复(1)
更多回答
public isExist(String s1, String s2) {
    if(s1 == null)
        return s2 == null;
    for(char c : s2.toCharArray()) {
        if(s1.charAt(c) == -1)
            return false;
    }
    return true;
}    
编辑于 2015-04-29 20:17:18 回复(1)
public static void main(String[] arg) {
        String string1 = "abx";
        String string2 = "abcdef";

        for (char b : string1.toCharArray())
            if (!string2.contains(b + ""))
                System.out.println(b);

    }
发表于 2015-04-27 15:46:32 回复(0)
    int primeTable[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409};
    
    string a = "abcdw";
    string b = "abcd";
    int ai = 1;
    int bi = 1;

    string::iterator iter = a.begin();
    for(;iter!= a.end();++iter)
    {
        unsigned char c = *iter - 'a';
        ai *= primeTable[c];
    }
    iter = b.begin();
    for(;iter!= b.end();++iter)
    {
        unsigned char c = *iter - 'a';
        bi *= primeTable[c];
    }
    cout << ai<<"," << bi << ",a%b == " << ai%bi << endl;   
楼下这个方案真棒,学习了
发表于 2015-04-24 18:37:58 回复(1)
这道题好像是goole还是什么大公司的面试题,以前我看过一篇博客,也是这个面试,他说最好的是:
定义26个质数,和26字母,用string1得到质数积,string2也得到质数积,然后%,看看能否除尽,能除尽说明string1包含string1,否则不包含,我试着写了下,大概是这样的,不过说实话,我没觉的这样有什么好的………………

public class SelfAudition {

    //质数集:网上复制
    private static int[] primeNumber={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409};
    //52字母,本来想用代码添加的,但是那样可能又要运算,更没效率
    private static char[] letter={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','M','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("start"+System.currentTimeMillis());
        System.out.println(IsContain("abx", "abcdef"));
        System.out.println(IsContain("qwertyu", "qwertyuiops"));
        System.out.println("aaend"+System.currentTimeMillis());
    }
    public static boolean IsContain(String child,String father){
        BigInteger fatherProduct=getProduct(father);//由于数据太大,这里用了BigInteger,没有大小限制的int包装类
        int childLen=child.length();
        BigInteger childProduct=new BigInteger("1");
        for (int i = 0; i < childLen; i++) {
            for (int j = 0; j < letter.length; j++) {
                if(letter[j]==(char)child.charAt(i)){
                    childProduct=childProduct.multiply(new BigInteger(primeNumber[j]+""));
                //    System.out.println(fatherProduct+"  "+childProduct+"  "+fatherProduct.remainder(childProduct).intValue());
                    if(fatherProduct.remainder(childProduct).intValue() != 0)//这里的判断可以不必要等child字符串都算完,有一个不包含,return false;
                        return false;
                    break;
                }
            }
        }
        return fatherProduct.remainder(childProduct).intValue() == 0;
    }
    public static BigInteger getProduct(String father){
        int len=father.length();
        BigInteger Product=new BigInteger("1");
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < letter.length; j++) {
                if(letter[j]==(char)father.charAt(i)){
                    Product=Product.multiply(new BigInteger(primeNumber[j]+""));
                    break;
                }
            }
        }
        return Product;
    }
}



发表于 2015-04-21 23:43:55 回复(0)
答:
题目要求判断string2中的每个字母在string1中是否存在,也就是判断string2中的每个字符是否能在string1中找到。较快的查找是二分查找,首先用string1构建一颗二叉查找树,然后分别找出string2的每个字母。设string1长度为m,string2长度为n,则时间复杂度为nlogm
编辑于 2015-03-21 11:18:41 回复(2)