share O(n^2) solution using Java!


  • 0
    T
    public class Solution {
        private int sum=0;
        public int countSubstrings(String s) {
            if(s==null || s.length()<1) return 0;
            char[] array=s.toCharArray();
            
            for(int i=0;i<s.length();i++){
                expand(array,i,i);
                if(i+1<array.length){
                    expand(array,i,i+1);
                }
            }
            return sum;
        }
        
        public void expand(char[] array,int left,int right){
            while(left>=0&&right<array.length){
                if(array[left]==array[right]){
                    sum++;
                    left--;
                    right++;
                }else{
                    break;
                }
            }   
        }
    }
    

Log in to reply
 

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