let count_pal s =
  let n = String.length s in
  let count = ref 0 in
  for c = 0 to 2 * n - 2 do
    let l = ref (c / 2) in
    let r = ref ((c + 1) / 2) in
    while !l >= 0 && !r < n && s.[!l] = s.[!r] do
      count := !count + 1;
      l := !l - 1;
      r := !r + 1
    done
  done;
  !count

;;

count_pal "aabaa"
