zoukankan      html  css  js  c++  java
  • Cracking the Coding Interview Q1.1

     Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

    My Solution:

    package chapter1;
    
    /**
     * Implement an algorithm to determine if a string has all unique characters.
     * What if you can not use additional data structures?
     * 
     * @author jd
     * 
     */
    public class Q1_1 {
    
        /**
         * if the char set is extended ASCII(256 characters).
         * 
         * Time complexity is O(n), where n is the length of the string, and space
         * complexity is O(1).
         * 
         */
        public static boolean isUniqueChars(String str) {
            if (str == null || str.length() <= 1)
                return true;
    
            boolean[] exist = new boolean[256];
            for (int i = 0; i < str.length(); i++) {
                if (exist[str.charAt(i)] == false)
                    exist[str.charAt(i)] = true;
                else
                    return false;
            }
            return true;
    
        }
    
        /**
         * we can reduce the space usage by using a bit vector
         * 
         */
        public static boolean isUniqueChars2(String str) {
            if (str == null || str.length() <= 1)
                return true;
            byte[] exist = new byte[32];
            for (int i = 0; i < str.length(); i++) {
                int idx = str.charAt(i);
                if ((exist[idx / 8] & (1 << (idx % 8))) == 0)
                    exist[idx / 8] |= 1 << (idx % 8);
                else
                    return false;
            }
            return true;
    
        }
    
        public static void main(String[] args) {
            String[] words = { "abcde", "hello", "apple", "kite", "padle", "aa", "abba" };
            for (String word : words) {
                System.out.println(word + ": " + isUniqueChars(word) + " " + isUniqueChars2(word));
            }
        }
    
        /**
         * alternative solutions: 
         * 1. Check each char of the string with every other
         * char of the string for duplicate occurrences. This will take O(n^2) and
         * no space. 
         * 2. If we are allowed to destroy the string, we can sort the
         * chars in the string in O(nlogn) time and linearly check the string for
         * neighboring chars that are identical. 
         */
    
    }
    View Code

    Solution:

    package Question1_1;
    
    public class Question {
    
        public static boolean isUniqueChars(String str) {
            if (str.length() > 256) {
                return false;
            }
            int checker = 0;
            for (int i = 0; i < str.length(); i++) {
                int val = str.charAt(i) - 'a';
                if ((checker & (1 << val)) > 0) return false;
                checker |= (1 << val);
            }
            return true;
        }
        
        public static boolean isUniqueChars2(String str) {
            if (str.length() > 256) {
                return false;
            }
            boolean[] char_set = new boolean[256];
            for (int i = 0; i < str.length(); i++) {
                int val = str.charAt(i);
                if (char_set[val]) return false;
                char_set[val] = true;
            }
            return true;
        }
        
        public static void main(String[] args) {
            String[] words = {"abcde", "hello", "apple", "kite", "padle"};
            for (String word : words) {
                System.out.println(word + ": " + isUniqueChars(word) + " " + isUniqueChars2(word));
            }
        }
    
    }
    View Code
  • 相关阅读:
    Docker搭建持续集成平台Jenkins
    Selenium Webdriver 架构
    JMeter性能监控系统:Jmeter + InfluxDB + Grafana
    持续集成平台Jenkins配置方法介绍
    perl中的map
    Smarty 配置文件中的相对路径
    挑出IIS日志里某一文件的请求次数
    写PHP,内伤中....
    File::Find
    强制删除删除不了的文件
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3821655.html
Copyright © 2011-2022 走看看