c - Write a function that return true if 2 passed strings to it are such that all characters of first string are uniquely present in second string -
efficient , o(n) code in c?? know solution of o(n*n)
stringcompare(str1, str2){ int freq1[100] = {0}, i; int freq2[100] = {0}; for(i=0; i<=strlen(str1); i++){ freq1[str1[i]]+ = 1; } for(i=0; i<=strlen(str2); i++) { freq2[str2[i]]+ = 1; } for(i=0;i<26;i++){ if(freq1[i]!=freq2[i]) return 0; return 1;
}
}
i modified mak
's pseudocode uses 1 frequency count array. positive value in final freq array means char in s1
not in s2
. negative value signals chars in s2
.
function same(s1,s2): freq=array of zeros i=0 length of s1: freq[s1[i]]+=1 i=0 length of s2: freq[s2[i]]-=1 i=0 alphabet_size: if not freq[i]=0 return "no" return "yes"
Comments
Post a Comment