Compare Strings

来源:互联网 发布:淘宝联盟买家已收货 编辑:程序博客网 时间:2024/06/05 15:18

Compare Strings

Compare two strings A and B, determine whether A contains all of the characters in B.

The characters in string A and B are all Upper Caseletters.

Example

For A = "ABCD", B = "ABC", return true.

For A = "ABCD" B = "AABC", return false.

Solution:

    public boolean compareStrings(String A, String B) {        if (A == null || B == null) {            return false;        }        if (A.length() < B.length()) {            return false;        }        if (A.length() >= 0 && B.length() == 0) {            return true;        }        boolean[] visited = new boolean[A.length()];        for (int i = 0; i < visited.length; i++) {            visited[i] = false;        }         int count = 0;        for (int i = 0; i < B.length(); i++) {            for (int j = 0; j < A.length(); j++) {                if (B.charAt(i) == A.charAt(j) && !visited[j]) {                    count++;                    if (count == B.length()) {                        return true;                    }                    visited[j] = true;                    break;                }            }        }        return false;    }
思路:

O(n^2)遍历,consumable 用visited标志。

0 0
原创粉丝点击