Assuming n and k are not too large to fit in memory:
You have a set with the first letter deleted, the second with the second letter deleted, the one with the third letter deleted, etc. Technically, this should be a map from lines to counts.
Run the list, just add the current item to each of the cards (obviously by deleting the corresponding letter first) (if it already exists, add the score to totalPairs and increase it by one).
Then totalPairs is the desired value.
EDIT:
Complexity:
It must be O(nklogn) .
You can use a map that uses hashing (e.g. HashMap in Java) instead of a sorted map for theoretical O(nk) complexity (although I usually found that the hash map will be slower than the sorted map-based tree).
Improvement:
A slight change on this is to have a card of the first two letters deleted by 2 cards, one with the first letter deleted and one with the second letter deleted, and have the same for the 3rd and 4th letters, etc. .
Then place them on cards with four letters deleted and on cards with 8 letters deleted, etc., up to half the letters deleted.
The complexity of this:
You perform 2 searches in 2 sorted sets containing a maximum of k elements (for each half).
For each of them, you again perform 2 searches in 2 sorted sets (for each quarter).
Thus, the number of searches is 2 + 4 + 8 + ... + k / 2 + k, which, in my opinion, is O(k) .
Perhaps I am mistaken, but in the worst case, the number of elements in any given map is n , but this will lead to the fact that all other maps will have only one element, therefore O(logn) , but for each n (not every nk ).
So, I think O(n.(logn + k)) .
.
EDIT 2:
Example of my cards (no improvement):
(x-1) means that x mapped to 1 .
Say we have abcd, abdd, adcb, adcd, aecd .
The first card will be (bcd-1), (bdd-1), (dcb-1), (dcd-1), (ecd-1) .
The second map will be (acd-3), (add-1), (acb-1) (for the 4th and 5th values already exists, therefore it increases).
The third card: (abd-2), (adb-1), (add-1), (aed-1) (the second already existed).
The fourth mapping: (abc-1), (abd-1), (adc-2), (aec-1) (the fourth already existed).
totalPairs = 0
For the second display - acd , for the fourth add 1, for the 5th add 2.
totalPairs = 3
For the third card - abd , for the 2nd add 1.
totalPairs = 4
For the fourth mapping - adc , for the fourth we add 1.
totalPairs = 5 .
A partial example of improved maps:
The same input as above.
The map of the first two letters deleted on the cards of the first and second letters has been deleted:
(cd-{ {(bcd-1)}, {(acd-1)} }), (dd-{ {(bdd-1)}, {(add-1)} }), (cb-{ {(dcb-1)}, {(acb-1)} }), (cd-{ {(dcd-1)}, {(acd-1)} }), (cd-{ {(ecd-1)}, {(acd-1)} })
The above map consists of a cd element displayed on 2 displays, one of which contains one element (bcd-1) and the other contains (acd-1) .
But for the 4th and 5th cd already exists, therefore, instead of generating the above, it will be added to this map as follows:
(cd-{ {(bcd-1, dcd-1, ecd-1)}, {(acd-3)} }), (dd-{ {(bdd-1)}, {(add-1)} }), (cb-{ {(dcb-1)}, {(acb-1)} })