Here is a DP version that might be easier to understand.

public class Solution { public int findMaxForm(String[] strs, int m, int n) { if(strs == null || strs.length == 0 || (m == 0 && n == 0)) return 0; // dp[k][n0][n1]: the max number of strings that can be added from the first k strings // (strs[0] to strs[k-1]), given n0 # of zeros and n1 # of ones. int dp [][][] = new int[strs.length+1][m+1][n+1]; for(int i = 1;i<=strs.length;i++){ // count the # of zeros and ones int[] count=new int[2]; for (char c:strs[i-1].toCharArray()) count[c-'0']++; // try to add this string for (int n0=0;n0<=m;n0++){ for (int n1=0;n1<=n;n1++){ if (count[0]>n0 || count[1]>n1){ // this string can't be added dp[i][n0][n1]=dp[i-1][n0][n1]; }else{ // can have two choices: skip it or add it // pick the one with the max count dp[i][n0][n1]=Math.max(dp[i-1][n0][n1], 1+dp[i-1][n0-count[0]][n1-count[1]]); } } } } return dp[strs.length][m][n]; } }Ones and Zeroes