Problem

    The maximum common string of the two strings

    The workaround

    First get the maximum number of characters to match the two strings

    Then get the comparison of the two strings of the side and the main string respectively what is

    Character matching is compared to the characters in it

    Character matching is compared to the characters in it

    Code implementation

    • Encapsulate Error
      1. class NoneError(Exception):
      2. def __str__(self):
      3. return f"The parameters participating in the operation cannot be empty, please check the parameter values"

    Function: String parameters that participate in the operation cannot be empty

    • The function code

      1. class get_Same_String:
      2. def __init__(self, str1=None, str2=None):
      3. self.__str1__ = str1
      4. self.__str2__ = str2
      5. self._get_max_len = 0
      6. self._get_example = None
      7. self._get_other = None
      8. @property
      9. def __Str1__(self):
      10. return self.__str1__
      11. @__Str1__.setter
      12. def __Str1__(self, Set_Str1):
      13. self.__str1__ = Set_Str1
      14. @property
      15. def __Str2__(self):
      16. return self.__str2__
      17. @__Str2__.setter
      18. def __Str2__(self, Set_Str2):
      19. self.__str2__ = Set_Str2
      20. @property
      21. def getLCString(self):
      22. return self.__get_LCString__()
      23. def __str_Nome_Error__(self):
      24. """
      25. :return String parameters that participate in the operation cannot be empty 进行参与运算的字符串参数不能为空
      26. """
      27. if not all([self.__str1__, self.__str2__]):
      28. raise NoneError
      29. def __max_len__(self):
      30. """
      31. :return The maximum lookup range 最大查找范围
      32. """
      33. return len(self.__str1__) if len(self.__str1__) < len(self.__str2__) else len(self.__str2__)
      34. def __example__(self):
      35. """
      36. :return:The main string for comparison 进行比较的主字符串
      37. """
      38. return self.__str1__ if len(self.__str1__) < len(self.__str2__) else self.__str2__
      39. def __other__(self):
      40. """
      41. :return The substring for comparison 进行比较的副字符串
      42. """
      43. return self.__str2__ if self.__str1__ == self._get_example else self.__str1__
      44. def __get_LCString__(self):
      45. self.__str_Nome_Error__()
      46. self._get_max_len = self.__max_len__()
      47. self._get_example = self.__example__()
      48. self._get_other = self.__other__()
      49. for _i in range(self._get_max_len):
      50. for _j in range(self._get_max_len, 0, -1):
      51. if self._get_other.find(self._get_example[_i: _j]) != -1:
      52. return self._get_example[_i: _j]
    • Code testing
      if __name__ == '__main__':
        new_getLCString = get_Same_String('abcdef', "bcdef")
        print(new_getLCString.getLCString)
        new_getLCString = get_Same_String(None, "bcdef")
        print(new_getLCString.getLCString)
      

    The result of the operation

    bcdef
    __main__.NoneError: The parameters participating in the operation cannot be empty, please check the parameter values