Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. public int numDecodings(String s) {
  2.     if(s==null||s.length()==0||s.equals("0"))
  3.         return 0;
  4.  
  5.  
  6.     int[] t = new int[s.length()+1];
  7.     t[0] = 1;
  8.  
  9.     //if(s.charAt(0)!='0')
  10.     if(isValid(s.substring(0,1)))
  11.         t[1]=1;
  12.     else
  13.         t[1]=0;
  14.  
  15.     for(int i=2; i<=s.length(); i++){
  16.         if(isValid(s.substring(i-1,i))){
  17.             t[i]+=t[i-1];
  18.         }
  19.  
  20.         if(isValid(s.substring(i-2,i))){
  21.             t[i]+=t[i-2];
  22.         }
  23.     }
  24.  
  25.     return t[s.length()];
  26. }
  27.  
  28. public boolean isValid(String s){
  29.     if(s.charAt(0)=='0')
  30.         return false;
  31.     int value = Integer.parseInt(s);
  32.     return value>=1&&value<=26;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement