C++, DP / state machine, O(n^2)
\n\n\nconst int N = 110;\n\nclass Solution {\n    int f[N][N][2];\npublic:\n    int countSubstrings(string s, string t) {\n        \n        int n = s.size(), m
![image](https://assets.leetcode.com/users/images/cfaf35fa-4079-49c6-8e87-e2c3cb2adb8a_1646522330.7448306.png)\n\n```\nconst int N = 110;\n\nclass Solution {\n    int f[N][N][2];\npublic:\n    int countSubstrings(string s, string t) {\n        \n        int n = s.size(), m = t.size();\n        for (int i = 1; i <= n; ++ i)\n            for (int j = 1;j <= m; ++ j)\n                if (s[i-1] == t[j-1]) f[i][j][0] = 1;\n                else f[i][j][1] = 1;\n        for (int i = 1; i <= n; ++ i)\n            for (int j = 1; j <= m; ++ j)\n            {\n                if (s[i-1] == t[j-1])\n                {\n                    f[i][j][1] += f[i-1][j-1][1];\n                    f[i][j][0] += f[i-1][j-1][0];\n                }\n                else\n                {\n                    f[i][j][1] += f[i-1][j-1][0];\n                }\n            }\n        \n        int ans = 0;\n        for (int i = 1; i <= n; ++ i)\n            for (int j = 1; j <= m; ++ j)\n            {\n                ans += f[i][j][1];\n            }\n        \n        return ans;\n            \n    }\n};\n```
[]