Solution by C#, using 'for' and substring


  • 0
    A
    public class Solution {
        public int CountSubstrings(string s) {
            string s_split="";
            bool pall=true;
            int count=0;
            for(int i=0;i<s.Length;i++)
            {
                for(int j=1;j<=s.Length-i;j++)
                {
                    s_split=s.Substring(i,j);
                    pall=true;
                    for(int m=0, n=s_split.Length-1;m<s_split.Length/2;m++,n--)
                    {
                       if(s_split[m]!= s_split[n])
                       {
                           pall=false;
                          break;
                       } 
                    }
                    if(pall)
                    {
                        count++;
                    }
                }
            }
            return count;
        }
    }
    

Log in to reply
 

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