[Python] Easy Pattern Detection with thought process when being asked during interviews
Pattern Detection\nThe difficulty of this problem come from 2 parts. For the sake of eay understanding, let\'s use i as the index of each part and X as the tota
### Pattern Detection\nThe difficulty of this problem come from 2 parts. For the sake of eay understanding, let\'s use `i` as the index of each part and `X` as the total number of parts.\n1. The total number of parts `X` is determined by the number of characters each part can hold which is various as index `i` increases. If `i` is less than `10`, the length of charactor the part can hold is `limit - 5`, if `i` is greater than `10` and less than `100`, the length is `limit - 6`.\n2. What maximum length of index `i` can have is determined by the `X`. \n\nSo, it seems both are dependent to each other. \nThen what we are going to do is to break the circular dependency by simplying the problem.\n\nLet\'s assume the `X` is less than `10`. \n### X <= 9\nthen the length of charactor each part holds is `limit - 5`\n```\nlen(message) <= 9 * (limit - 5) == 9 * limit - 9 * 5\n```\n\n### X <= 99\nwe know it must contain the first 9 parts, so we can simply subtract the first 9 parts and caculate how many parts we can have so that `X` is less than `100`.\nNote, since the `X` is with 2 digits, so it is `limit - 6` for the first 9 parts instead of `limit - 5`.\n```\nlen(message) - 9 * (limit - 6) <= 90 * (limit - 7) == 99 * limit - 90 * 7 - 9 * 6\n```\n\n### X <= 999\nSimilarly, we know the first 99 parts must be included, so we subtract them and caculuate the `X`\n```\nlen(message) - 9 * (limit - 7) - 90 * (limit - 8) <= 900 * (limit - 9) == 900 * limit - 900 * 9 - 90 * 8 - 9 * 7\n```\n\nDo you see the pattern? leaving the part for `X <= 9999` for you to figure out.\nTherefore, we can simply check which range the length of `message` falls in and know the length of `X`, which helps to split the `message`.\n\n\n```python\ndef splitMessage(self, message: str, limit: int) -> List[str]:\n\n\tif limit < 6:\n\t\treturn []\n        \n\tdef process(length_x):\n\t\tindex = 1\n        i = 0\n        pieces = []\n        while i < n:\n\t\t\tlength = 3 + length_x + len(str(index)) \n\t\t    pieces.append((message[i: i + (limit - length)], str(index)))\n            i += limit - length\n            index += 1\n\t\treturn [\'\'.join([piece[0], \'<\', piece[1], \'/\', str(index - 1), \'>\']) for piece in pieces]     \n                \n\tn = len(message)\n\tif n <= 9 * limit - 9 * 5:\n\t\treturn process(1)\n\tif n <= 99 * limit - 90 * 7 - 9 * 6: \n\t   return process(2)\n\tif n <= 999 * limit - 900 * 9 - 90 * 8 - 9 * 7:\n\t\treturn process(3)\n\tif n <= 9999 * limit - 9000 * 11 - 900 * 10 - 90 * 9 - 9 * 8:\n\t\treturn process(4)\n\treturn []\n```\n\n*Time Complexity* = **O(N)**\n*Space Complexity* = **O(N)**\n
[]