public int numDistinct(String S, String T) {
		int n = S.length();
		int m = T.length();
		if (n < m)
			return 0;
		int[][] result = new int[n + 1][m + 1];

		for (int i = 0; i <= n; i++) {
			result[i][0] = 1;
		}

		for (int r = 1; r <= n; r++) {
			for (int c = 1; c <= m; c++) {
				if (S.charAt(r - 1) == T.charAt(c - 1)) {
					result[r][c] = result[r - 1][c] + result[r - 1][c - 1];
				} else
					result[r][c] = result[r - 1][c];
			}
		}
		return result[n][m];
	}