Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public class Solution {
  2. public int numDistinct(String s, String t) {
  3. int length = s.length();
  4. int width = t.length();
  5. if(length==0||width==0) return 0;
  6. int[][] dp = new int[length][width];
  7. int count = 0;
  8. for(int i = 0; i < length; i++)
  9. {
  10. if(s.charAt(i)==t.charAt(0)) count++;
  11. dp[i][0]=count;
  12. }
  13. for(int i = 1; i < length; i++)
  14. {
  15. for(int j = 1; j < width; j++)
  16. {
  17. if(s.charAt(i)==t.charAt(j))
  18. {
  19. dp[i][j] = dp[i-1][j-1]+dp[i-1][j];
  20. }else
  21. {
  22. dp[i][j]=dp[i-1][j];
  23. }
  24. }
  25. }
  26. return dp[length-1][width-1];
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement