Passed 128 out of 130 test cases


  • 0
    G

    ...
    class Solution {
    private static boolean isPalindrome(String s) {
    if(s==null || s.trim().equalsIgnoreCase("")) {
    return false;
    }

    	int j = s.length()-1;
    	
    	boolean retValue = true;
    	for(int i=0;i<s.length();i++) {
    		char beg = s.charAt(i);
    		char end = s.charAt(j);
    		if(beg!=end) {
    			retValue=false;
    			break;
    		}
    		j--;
    	}
    	
    	return retValue;
    }
    
    
    
    public int countSubstrings(String input) {
    	int palCounter=0;
    	List<String> comb = new ArrayList<String>(); 
    	for(int i =0;i<input.length();i++) {
    		makeCombinations(comb, i, input);
    	}	
    	
    	comb.add(input);
    	
    	for(int j=0;j<comb.size();j++) {
    		String combination = comb.get(j);
    		if(isPalindrome(combination)) {
    			++palCounter;
    		}			
    	
    	}
    	
    	return palCounter;
    }
    
    
    public void makeCombinations(List<String> inputList,int begCounter,String inputStr) {
    	
    	int endCounter = begCounter+1;
    	String subs = null;
    	if(endCounter==inputStr.length()) {
    		endCounter = inputStr.length()-1;
    	}
    	
    	if(begCounter==endCounter) {
    		return;
    	}
    	
    	int i=0;
    	boolean incrementStopped =false;
    	while(i<inputStr.length()) {
    		subs = inputStr.substring(i,endCounter);
    		
    		if(!incrementStopped) {
    			inputList.add(subs);
    		}
    		
    		
    		if(endCounter<inputStr.length()) {
    			endCounter++;	
    		}
    		else {
    			incrementStopped=true;
    		}
    		i++;
    	}
    	
    	
    	
    }
    

    }
    ...


Log in to reply
 

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