Java 11ms solution


  • 0
    Y
    class Solution {
        public int countSubstrings(String s) {
            if(s.length()==1)
                return 1;
            if(s.length()==2)
            {
                if(s.charAt(0)==s.charAt(1))
                    return 3;
                else
                    return 2;
            }
            int res=0;
            char first=s.charAt(0);
            int begin=0;
            int end;
            int flag=1;
            while(flag<s.length())
            {
                if(s.charAt(flag)==first)
                {
                    flag++;
                    continue;
                }
                else
                    break;
            }
            int para=flag-1;
            res+=(flag-1)*flag*0.5;
            //for case  s="aaaaa";
            if(flag==s.length())
                return res+s.length();
            //for case s="aaaab",first='a',sec='b',begin=0,end=4;
            char sec=s.charAt(flag);
            end=flag;
            int i=flag+1;
            int firOrSecRepeat=0;//1 means first Repeat,2 sec repeat
            int lengthRepeat=flag-1;
            if(lengthRepeat>0)
                firOrSecRepeat=1;
    
            while(i<s.length())
            {
                if(s.charAt(i)!=first && s.charAt(i) !=sec) //like case s="abcd" first='a' sec='b' i=2;
                {
                    first=sec;
                    begin=end; //indexOf first
                    para=0;
                    lengthRepeat=0;
                    firOrSecRepeat=2; // 1 impossible
                    sec=s.charAt(i);
                    end=i;//indexOf sec
                    //go on fir='b' sec='c'
                    i++;
                }
                else if(s.charAt(i)==first) //case: s="aba"
                {
                    res++;
                    int temp2=i+1; //first=c,begin=0,sec=b,end=2;
                    int temp1=begin-1;
                    if(firOrSecRepeat==1)//s="ccbcc"
                        temp1+=lengthRepeat; //temp1=1,temp2=3...
                    while(temp1 >= 0 && temp2 < s.length())
                    {
                        if(s.charAt(temp1)==s.charAt(temp2))
                        {
                            res++;
                            temp1--;
                            temp2++;
                            continue;
                        }
                        else
                            break;
                    }
                    first=sec;  //first='b' sec='c'
    
                    begin=end;
                    lengthRepeat=0;
                    sec=s.charAt(i);
                    end=i;
                    i++;
                }    
                else if(s.charAt(i)==sec) // if s="baaaa"
                {
    
                    int numSecRepeat=1;
                    while(i<s.length())
                    {
                        if(s.charAt(i)==sec)
                        {
    
                            numSecRepeat++;
                            i++;
                            continue;
                        }
                        else
                            break;
                    }
                    lengthRepeat=numSecRepeat-1;
                    firOrSecRepeat=2;
                    res += numSecRepeat*(numSecRepeat-1)*0.5;
                    end=i-1;
                }
                else
                {
                    i++;
                }
            }
            return res+s.length();
        }
    }
    
    

Log in to reply
 

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