Two of my test cases fail. Can someone please help ?


  • 0
    O

    Here is my code

    public class Solution {
             boolean isPalin(String s){
                int start = 0;
                int end = s.length()-1;
                while(start<=end){
                    if(s.charAt(start) != s.charAt(end)){
                        return false;
                    }
                    start++;
                    end--;
                }
                return true;
            }
            
            HashMap<String, Integer> map = new HashMap<String, Integer>();
           
            public int minCut(String s) {
                int min = s.length()-1;
                if(isPalin(s)){
                    return 0;
                }
                
                int k=0;
                while(k<s.length() && s.charAt(k)==s.charAt(k+1)){
                    if(isPalin(s.substring(k+1))){
                        break;
                    }
                    k++;
                }
                
                 for(int i=k+1; i<=s.length(); i++){
                    String pre = s.substring(0,i);
                    if(isPalin(pre)){
                        if(map.containsKey(s.substring(i))){
                            min = Math.min(1+map.get(s.substring(i)), min);
                        }
                        else{
                            min = Math.min(1+minCut(s.substring(i)), min);
                        }
                    }
                 }
                
                map.put(s, min);
                return min;
            }
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.