python dp O(MN) and O(N) memory
\nclass Solution:\n    def countSubstrings(self, s: str, t: str) -> int:\n        m,n = len(s), len(t)\n        A, B = [0]*(n+1), [0]*(n+1)\n        ans = 0\n  
```\nclass Solution:\n    def countSubstrings(self, s: str, t: str) -> int:\n        m,n = len(s), len(t)\n        A, B = [0]*(n+1), [0]*(n+1)\n        ans = 0\n        for i in range(m):\n            AA, BB = [0]*(n+1), [0]*(n+1)\n            for j in range(n):\n                AA[j+1] = 1 + A[j] if s[i]==t[j] else 0\n                BB[j+1] = B[j] if s[i]==t[j] else 1 + A[j]\n                ans += BB[j+1]\n            A, B = AA, BB\n        return ans\n```
['Python3']