I am writing a c program to find the number of common characters in two lines.
For example: aabbccc aabc Ans: 4 aabcA aa Ans:2
For example: aabbccc aabc Ans: 4
aabcA aa Ans:2
(Strings will be uppercase, lowercase, and numbers)
I have two algorithms in my mind
Assuming the string length is n, m
1.Sort arrays and then calculate O complexity (nlogn + mlogm)
2.scan through two lines and use count arrays - O (n + m) complexity
Can anyone suggest further optimization or any other methods for this?
basically you ask a question about the intersection Bag (Multiset) .
, , O (n + m), .
, , ( ). , , . , , O (m + n). , , O (26 + 26 + 10), . , . google " - ++" .