Decrease the cost of two lists comparisons
<p>I want to minimize the cost of comparisons of two lists that have some words. In the code below <code>A</code> has 4 words whereas <code>B</code> has 2 words and the cost is <code>O(n^2)</code> that is too bad. While for 100 words it can be time consuming. Can I minimize it somehow?</p>

<pre><code>A= ["helry", "john" , "kat" , "david"]
d="Helry David"
B = d.lower().split()

for x in range(len(A)):
            for i in range(len(B)):
                if A[x] == B[i]:
                    print("Match = " + A[x])
                else:
                    print("No")
</code></pre>

<ol>
<li><code>O(nlogn)</code> for sorting <code>A</code>. </li>
<li>use a binary search for each
element of <code>B</code> in <code>A</code> Iit'll take <code>O(log n)</code>. For <code>m</code> elements in
<code>B</code>, it'll be <code>O(m*logn)</code>.</li>
</ol>

<p>(or) if you use hash, you can do in <code>O(m)</code> for <code>m</code> elements of <code>B</code>.</p>

python time-complexity