The key idea is sort the elements in strs by their length. When there is a tie, choose one with smaller difference between the number of "0" and "1" to be the preferable choice.

```
class Solution(object):
def findMaxForm(self, strs, m, n):
"""
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
A = sorted(strs, key = lambda x : (len(x), - x.count("0") * x.count("1")))
ans = 0
for i in A:
M, N = i.count("0"), i.count("1")
if M <= m and N <= n:
ans += 1
m -= M
n -= N
return ans
```