[Java/C++/Python] Time O(nm), Space O(1)
Intuition\nChoose a start point for s and a start point for t, and then compare characters one by one.\n\n\n# Explanation\nTry to match from s[i] and t[j],\nwhe
# **Intuition**\nChoose a start point for `s` and a start point for `t`, and then compare characters one by one.\n<br>\n\n# **Explanation**\nTry to match from s[i] and t[j],\nwhere i = 0 or j = 0.\n\n`cur` is the current number of consecutive same characters.\n`pre` is the previous number of consecutive same characters.\n<br>\n\n# **Complexity**\nTime `O(mn)`\nSpace `O(1)`\n<br>\n\n**Java**\n```java\n    public int countSubstrings(String s, String t) {\n        int res = 0 ;\n        for (int i = 0; i < s.length(); ++i)\n            res += helper(s, t, i, 0);\n        for (int j = 1; j < t.length(); ++j)\n            res += helper(s, t, 0, j);\n        return res;\n    }\n\n\n    public int helper(String s, String t, int i, int j) {\n        int res = 0, pre = 0, cur = 0;\n        for (int n = s.length(), m = t.length(); i < n && j < m; ++i, ++j) {\n            cur++;\n            if (s.charAt(i) != t.charAt(j)) {\n                pre = cur;\n                cur = 0;\n            }\n            res += pre;\n        }\n        return res;\n    }\n```\n**C++**\n```cpp\n    int countSubstrings(string s, string t) {\n        int res = 0 ;\n        for (int i = 0; i < s.length(); ++i)\n            res += helper(s, t, i, 0);\n        for (int j = 1; j < t.length(); ++j)\n            res += helper(s, t, 0, j);\n        return res;\n    }\n\n    int helper(string s, string t, int i, int j) {\n        int res = 0, pre = 0, cur = 0;\n        for (int n = s.length(), m = t.length(); i < n && j < m; ++i, ++j) {\n            cur++;\n            if (s[i] != t[j])\n                pre = cur, cur = 0;\n            res += pre;\n        }\n        return res;\n    }\n```\n\n**Python:**\n```py\n    def countSubstrings(self, s, t):\n        n, m = len(s), len(t)\n\n        def test(i, j):\n            res = pre = cur = 0\n            for k in xrange(min(n - i, m - j)):\n                cur += 1\n                if s[i + k] != t[j + k]:\n                    pre, cur = cur, 0\n                res += pre\n            return res\n        return sum(test(i, 0) for i in xrange(n)) + sum(test(0, j) for j in xrange(1, m))\n```\n
[]