Quantcast
Channel: 书影 - Entries for the tag leetcode
Viewing all articles
Browse latest Browse all 559

[LeetCode]Minimum Unique Word Abbreviation

$
0
0

题目描述:

LeetCode 411. Minimum Unique Word Abbreviation

A string such as "word" contains the following abbreviations:

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

Given a target string and a set of strings in a dictionary, find an abbreviation of this target string with the smallest possible length such that it does not conflict with abbreviations of the strings in the dictionary.

Each number or letter in the abbreviation is considered length = 1. For example, the abbreviation "a32bc" has length = 4.

Note:

In the case of multiple answers as shown in the second example below, you may return any one of them.
Assume length of target string = m, and dictionary size = n. You may assume that m ≤ 21, n ≤ 1000, and log2(n) + m ≤ 20.

Examples:

"apple", ["blade"] -> "a4" (because "5" or "4e" conflicts with "blade")

"apple", ["plain", "amber", "blade"] -> "1p3" (other valid answers include "ap3", "a3e", "2p2", "3le", "3l1").

题目大意:

一个字符串"word"可以包含下列缩写:

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

给定一个目标串和一个字符串集合组成的字典,寻找目标串的缩写的最小可能长度,并且该缩写不与字典中的字符串的缩写相互冲突。

缩写中的每一个数字或者字母的长度都认为是1。例如缩写“a32bc”的长度是4。

注意:

在有多种答案时,比如第二组测试用例,你可以任选其一返回。

假设目标串长度 = m,字典长度 = n。你可以假设m ≤ 21, n ≤ 1000, 并且 log2(n) + m ≤ 20。

解题思路:

深度优先搜索(DFS) + 剪枝

将单词缩写abbr利用二进制转化为数字形式,例如"w3d"可以视为二进制数10001,即十进制17。

检测单词缩写abbr与字典dictionary中的单词d是否存在冲突,可以通过如下方式判断:

for d in dictionary:
    abbr & d == abbr

若循环中存在上式为真的情形,说明存在冲突。

DFS从target出发,逐一去除字母,检测冲突,若不存在冲突,则递归,并更新最优解。

剪枝策略:

利用变量len记录当前时刻的最优的单词缩写长度,若DFS分支的长度大于len,则可剪枝。

Python代码:

class Solution(object):
    def minAbbreviation(self, target, dictionary):
        """
        :type target: str
        :type dictionary: List[str]
        :rtype: str
        """
        self.size = len(target)
        self.wlist = [self.toNumber(target, d) \
                      for d in dictionary \
                      if len(d) == self.size]
        self.ans = (1 << self.size) - 1
        self.len = self.size + 1
        self.vset = set([self.ans])
        self.dfs(self.ans, self.size)
        return self.toWord(self.ans)
    def dfs(self, number, depth):
        if depth >= self.len: return
        if not self.checkNumber(number): return
        self.ans = number
        self.len = depth
        for x in range(self.size):
            if (number >> x) & 1:
                next = number ^ (1 << x)
                if next not in self.vset:
                    self.vset.add(next)
                    self.dfs(next, depth - 1)
    def toNumber(self, target, word):
        ans = 0
        for x in range(self.size - 1, -1, -1):
            ans <<= 1
            ans += target[x] == word[x]
        return ans
    def toWord(self, number):
        ans = ''
        cnt = 0
        for x in range(self.size):
            if number & 1:
                if cnt:
                    ans += str(cnt)
                    cnt = 0
                ans += target[x]
            else:
                cnt += 1
            number >>= 1
        if cnt:
            ans += str(cnt)
        return ans
    def checkNumber(self, number):
        for w in self.wlist:
            if number & w == number:
                return False
        return True

 


Viewing all articles
Browse latest Browse all 559

Latest Images

Trending Articles



Latest Images