leetcode - 527. Word Abbreviation

Description

Given an array of distinct strings words, return the minimal possible abbreviations for every word.

The following are the rules for a string abbreviation:

  1. The initial abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.
  2. If more than one word shares the same abbreviation, then perform the following operation:
    • Increase the prefix (characters in the first part) of each of their abbreviations by 1.
      • For example, say you start with the words [“abcdef”,“abndef”] both initially abbreviated as “a4f”. Then, a sequence of operations would be [“a4f”,“a4f”] -> [“ab3f”,“ab3f”] -> [“abc2f”,“abn2f”].
    • This operation is repeated until every abbreviation is unique.
  3. At the end, if an abbreviation did not make a word shorter, then keep it as the original word.

Example 1:

Input: words = ["like","god","internal","me","internet","interval","intension","face","intrusion"]
Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]

Example 2:

Input: words = ["aa","aaa"]
Output: ["aa","aaa"]

Constraints:

1 <= words.length <= 400
2 <= words[i].length <= 400
words[i] consists of lowercase English letters.
All the strings of words are unique.

Solution

Just code intuitively… Write a function for doing the abbreviation, and then use a dict, with the key as the abbreviation, and value as the list of common words indexes. Every time add the list with length 1, and keep expanding those list with length more than 1.

Code

class Solution:
    def wordsAbbreviation(self, words: List[str]) -> List[str]:
        def abbrev(s: str, prefix_len: int) -> str:
            abbr = f'{
     s[:prefix_len]}{
     len(s) - prefix_len - 1}{
     s[-1]}'
            if len(abbr) >= len(s):
                abbr = s
            return abbr
        # key: abbr, value: [index1, index2, ...]}
        abbr_info = {
   }
        cur_prefix = 1
        # init abbr_info
        for i in range(len(words)):
            abbr = abbrev(words[i], cur_prefix)
            if abbr not in abbr_info:
                abbr_info[abbr] = []
            abbr_info[abbr].append((i, abbr))
        res = []
        while abbr_info:
            new_abbr_info = {
   }
            cur_prefix += 1
            for each_abbr, same_list in abbr_info.items():
                if len(same_list) == 1:
                    res.append(same_list[0])
                else:
                    for index, _ in same_list:
                        new_abbr = abbrev(words[index], cur_prefix)
                        if new_abbr not in new_abbr_info:
                            new_abbr_info[new_abbr] = []
                        new_abbr_info[new_abbr].append((index, new_abbr))
            abbr_info = new_abbr_info
        res.sort(key=lambda x:x[0])
        return list(item[1] for item in res)

相关推荐

  1. leetcode - 527. Word Abbreviation

    2024-01-24 14:46:03       51 阅读
  2. Leetcode 537. 复数乘法

    2024-01-24 14:46:03       132 阅读
  3. leetcode 547.省份数量

    2024-01-24 14:46:03       28 阅读
  4. 字符串的排列(LeetCode 567

    2024-01-24 14:46:03       38 阅读
  5. LeetCode 567. 字符串的排列

    2024-01-24 14:46:03       45 阅读
  6. LeetCode //C - 547. Number of Provinces

    2024-01-24 14:46:03       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 14:46:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 14:46:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 14:46:03       87 阅读
  4. Python语言-面向对象

    2024-01-24 14:46:03       96 阅读

热门阅读

  1. Spring/Spring boot项目接入traceId

    2024-01-24 14:46:03       45 阅读
  2. C Primer Plus(第六版)13.11 编程练习 第11题

    2024-01-24 14:46:03       48 阅读
  3. Vue学习笔记11--路由2(路由传参/命名路由)

    2024-01-24 14:46:03       45 阅读
  4. 课堂练习3.4:进程的切换

    2024-01-24 14:46:03       49 阅读
  5. 互动直播项目 梳理 自定义视频帧控件 BitmapControl

    2024-01-24 14:46:03       52 阅读
  6. 在Spring Boot中整合MyBatis

    2024-01-24 14:46:03       61 阅读
  7. 什么是lustre文件系统

    2024-01-24 14:46:03       53 阅读
  8. vue响应式原理

    2024-01-24 14:46:03       58 阅读