Skip to content

Latest commit

 

History

History
27 lines (25 loc) · 738 Bytes

1638.md

File metadata and controls

27 lines (25 loc) · 738 Bytes

1638. Count Substrings That Differ by One Character

Solution 1 (time O(mnmin(m,n)), space O(1))

class Solution(object):
    def countSubstrings(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: int
        """
        ans = 0
        for i in range(len(s)):
            for j in range(len(t)):
                diff = 0
                k = 0
                while i + k < len(s) and j + k < len(t):
                    if s[i + k] != t[j + k]:
                        diff += 1
                    if diff == 1:
                        ans += 1
                    elif diff > 1:
                        break
                    k += 1
        return ans