input: Write a function to find the longest common substring in two given strings. If there are multiple substrings of the same length, return the first one encountered. If either string is empty, return an empty string as the result.
  assert longestCommonSubstring("abcdef", "zabcaef")=="abc"
  assert longestCommonSubstring("python", "typhoon")=="yph"
  assert longestCommonSubstring("algorithm", "logarithm")=="lgo"
  assert longestCommonSubstring("hello", "")==""
  assert longestCommonSubstring("", "")==""
output: def longestCommonSubstring(str1, str2):
      if not str1 or not str2:
          return ""
      
      m, n = len(str1), len(str2)
      max_len = 0
      end_index = 0
      dp = [[0] * (n + 1) for _ in range(m + 1)]
      
      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
      
      return str1[end_index - max_len:end_index]