Easiest Solution Ever in constant space


#1

Simply increase the count of the character found for A and B, while decrease count in case of string C.
Check if the count for all chars is 0 or not.
Since there are only 128 chars possible, it is a constant space solution.

Here is my Code:

int Solution::isInterleave(string A, string B, string C) {
vector count(128,0);

for(int i=0;i<A.length();i++) count[(int)A[i]]++;
for(int i=0;i<B.length();i++) count[(int)B[i]]++;
for(int i=0;i<C.length();i++) count[(int)C[i]]--;

for(int i=0;i<128;i++) if(count[i] != 0) return 0;
return 1;

}

Enjoy!!


#2

i think this will only ensure presence of characters not forming c by interleaving A and B ,
what about if A=“abcdef” and B=“lmn” and C="lmaebcndf " your function most probably will return 1,
but actually C can’t be formed by interleaving A and B,are u agreee???


#3

Yes, I found the mistake brother.
Still, thanks for replying.