Algo to find the number of common characters in 2 lines

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

(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?

+3
source share
2 answers

basically you ask a question about the intersection Bag (Multiset) .

, , O (n + m), .

+1

, , ( ). , , . , , O (m + n). , , O (26 + 26 + 10), . , . google " - ++" .

0

All Articles