Detecting if a string has unique characters: comparing my solution to “Cracking the Coding Interview?”

后端 未结 7 1848
孤街浪徒
孤街浪徒 2020-12-22 16:43

I am working through the book \"Cracking the Coding Interview\" and I have come across questions here asking for answers, but I need help comparing my answer to the solution

7条回答
  •  我在风中等你
    2020-12-22 17:29

    The book solution is one I don't like and I believe is dysfunctional..... templatetypedef has posted a comprehensive answer which indicates that the solution is a good one. I disagree, since the book's answer assumes that the string only has lower-case characters, (ascii) and does no validation to ensure that.

    public static boolean isUniqueChars(String str) {
        // short circuit - supposed to imply that
        // there are no more than 256 different characters.
        // this is broken, because in Java, char's are Unicode,
        // and 2-byte values so there are 32768 values
        // (or so - technically not all 32768 are valid chars)
        if (str.length() > 256) {
            return false;
        }
        // checker is used as a bitmap to indicate which characters
        // have been seen already
        int checker = 0;
        for (int i = 0; i < str.length(); i++) {
            // set val to be the difference between the char at i and 'a'
            // unicode 'a' is 97
            // if you have an upper-case letter e.g. 'A' you will get a
            // negative 'val' which is illegal
            int val = str.charAt(i) - 'a';
            // if this lowercase letter has been seen before, then
            // the corresponding bit in checker will have been set and
            // we can exit immediately.
            if ((checker & (1 << val)) > 0) return false;
            // set the bit to indicate we have now seen the letter.
            checker |= (1 << val);
        }
        // none of the characters has been seen more than once.
        return true;
    }
    

    The bottom line, given templatedef's answer too, is that there's not actually enough information to determine whether the book's answer is right.

    I distrust it though.

    templatedef's answer on the complexity is one I agree with though.... ;-)

    EDIT: As an exercise, I converted the book's answer to one that will work (albeit slower than the book's answer - BigInteger is slow).... This version does the same logic as the book's, but does not have the same validation and assumption problems (but it is slower). It is useful to show the logic too.

    public static boolean isUniqueChars(String str) {
        if (str.length() > 32768) {
            return false;
        }
        BigInteger checker = new BigInteger(0);
        for (int i = 0; i < str.length(); i++) {
            int val = str.charAt(i);
            if (checker.testBit(val)) return false;
            checker = checker.setBit(val);
        }
        // none of the characters has been seen more than once.
        return true;
    }
    

提交回复
热议问题