Java solution with comments


  • 0
    J

    class Solution {
    public int countSubstrings(String s) {
    //ans will be stored in count
    int count=0;
    //set starting index of substring
    for(int i=0;i<s.length();i++)
    {
    // f will store subsrtring
    String f="";
    // p will store reverse of f
    String p="";
    // find every substring starting with i
    for(int j=i;j<s.length();j++)
    {
    f+=s.charAt(j);
    p=s.charAt(j)+p;
    if(f.equals(p))
    count++;
    }
    }
    return count;

    }
    

    }


Log in to reply
 

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