写出一个Python函数，该函数可以接受两个字符串作为参数，并返回两个字符串中最长的公共子串。


str1 = 'abcdefg'
str2 = 'acdeghj'
```python
def longest_common_substring(str1, str2):
    """
    :param str1: First string
    :param str2: Second string
    :return: Longest common substring
    """
    m = len(str1)
    n = len(str2)
    # Initialize a dynamic programming matrix
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    # Variables to keep track of the longest matching substring and its end index
    max_len = 0
    end_index = 0
    # Fill the dynamic programming matrix
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if str1[i - 1] == str2[j - 1]:
                dp[i][j] = dp[i - 1][j - 1] + 1
                if dp[i][j] > max_len:
                    max_len = dp[i][j]
                    end_index = i
            else:
                dp[i][j] = 0
    # Return the longest common substring
    return str1[end_index - max_len: end_index]
```

使用示例：

```python
str1 = 'abcdefg'
str2 = 'acdeghj'
print(longest_common_substring(str1, str2))  # 输出：'cde'
```

在这个例子中，输入的两个字符串“ABCDEFG”和“ACDEGHJ”中最长的公共子串为“CDE”。