Lintcode 比较字符串

    xiaoxiao2021-03-25  110

    比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母

     注意事项

    在 A 中出现的 B 字符串里的字符不需要连续或者有序。

    您在真实的面试中是否遇到过这个题?  Yes 样例

    给出 A = "ABCD" B = "ACD",返回 true

    给出 A = "ABCD" B = "AABC", 返回 false

    class Solution { public: /** * @param A: A string includes Upper Case letters * @param B: A string includes Upper Case letter * @return: if string A contains all of the characters in B return true * else return false */ bool compareStrings(string A, string B) { // write your code here if(B.empty()) return true; int s[27]={0}; int n=A.size(); int m=B.size(); for(int i=0;i<n;i++){ s[(A[i]-'A')]++; } for(int i=0;i<m;i++){ if(s[B[i]-'A']>0){ s[B[i]-'A']--; } else return false; } return true; } };

    转载请注明原文地址: https://ju.6miu.com/read-22303.html

    最新回复(0)