运行时这个字符串算法的复杂性?

时间:2018-01-09 07:02:59

标签: string algorithm big-o

给定字符串a和b,我试图理解charDifference方法的大O:

public static int charDifference(String first, String second) {
    int steps = 0;
    HashMap<Character, Integer> first_histogram = getFrequencies(first);
    HashMap<Character, Integer> second_histogram = getFrequencies(second);

    Set<Character> first_keys = first_histogram.keySet(); // O(N)
    Set<Character> second_keys = second_histogram.keySet(); // O(N)

    // get first_histogram keys and loop through second_histogram to see what the differences are
        // add differences to steps
    for (char first_char : first_keys){
        int first_count = first_histogram.get(first_char);
        if (second_histogram.containsKey(first_char)){
            int second_count = second_histogram.get(first_char);
            if (first_count > second_count){
                steps += first_count - second_count;
            } else if (first_count < second_count){
                steps += second_count - first_count;
            }
        } else {
            steps += first_count;
        }
    }
    // if this key isn't in second_histogram, then add the count to steps
    // loop through second_histogram keys and if the key isn't in first_histogram, add the count to steps
    for (char second_char : second_keys){
        int second_count = second_histogram.get(second_char);
        if (!(first_histogram.containsKey(second_char))){
            steps += second_count;
        }
    }
    return steps;
}

private static HashMap<Character,Integer> getFrequencies(String str) {
    HashMap<Character, Integer> histogram = new HashMap<Character,Integer>();
    for (int i = 0; i < str.length(); i++){
        char current = str.charAt(i);
        if (histogram.containsKey(current)){
            int count = histogram.get(current);
            histogram.put(current, count+1);
        } else {
            histogram.put(current, 1);
        }
    }
    return histogram;
}

我得到了O(N ^ 2),因为我为两个字符串调用了getFrequencies函数,并遍历每个集合来更新steps变量。这是正确的吗?我该怎么做才能降低复杂性?

1 个答案:

答案 0 :(得分:0)

回答你的实际问题(不是我在各行之间阅读的那些问题!):

  1. 您的分析不正确,因为它甚至没有定义N。即使假设这是输入的长度,它仍然会提出应该是哪个输入的问题,如已经评论过的那样,因为涉及两个序列。

  2. 为了减少复杂性的上限,您可以使用基于树的映射而不是哈希映射。前者可以为您提供O(log n)进行查找,而后者则需要O(n)。

  3. 我想这对你没有多大帮助,即使它能回答你的问题。我会考虑问你关心的是什么,但你必须首先决定。例如,询问某些事情是否正确无济于事。解释你的分析并询问其他人在哪些方面不正确会更加明智。