[LintCode]Compare Strings(C++|Python)

xiaoxiao2021-02-28  101

C++ Version:

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 int count[2][26] = {0}; for (int i = 0; i < A.length(); i++) { count[0][A[i] - 'A']++; } for (int i = 0; i < B.length(); i++) { count[1][B[i] - 'A']++; } for (int i = 0; i < 26; i++) { if (count[0][i] < count[1][i]) { return false; } } return true; } };

Python Version:

class Solution: """ @param A : A string includes Upper Case letters @param B : A string includes Upper Case letters @return : if string A contains all of the characters in B return True else return False """ def compareStrings(self, A, B): # write your code here a = list(A) b = list(B) for c in b: if c not in a: return False a.remove(c) return True

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

最新回复(0)