Skip to content

Latest commit

 

History

History
215 lines (165 loc) · 6.44 KB

File metadata and controls

215 lines (165 loc) · 6.44 KB

English Version

题目描述

单词的 缩写 需要遵循 <起始字母><中间字母数><结尾字母> 这样的格式。如果单词只有两个字符,那么它就是它自身的 缩写

以下是一些单词缩写的范例:

  • dog --> d1g 因为第一个字母 'd' 和最后一个字母 'g' 之间有 1 个字母
  • internationalization --> i18n 因为第一个字母 'i' 和最后一个字母 'n' 之间有 18 个字母
  • it --> it 单词只有两个字符,它就是它自身的 缩写

 

实现 ValidWordAbbr 类:

  • ValidWordAbbr(String[] dictionary) 使用单词字典 dictionary 初始化对象
  • boolean isUnique(string word) 如果满足下述任意一个条件,返回 true ;否则,返回 false
    • 字典 dictionary 中没有任何其他单词的 缩写 与该单词 word缩写 相同。
    • 字典 dictionary 中的所有 缩写 与该单词 word缩写 相同的单词都与 word 相同

 

示例:

输入
["ValidWordAbbr", "isUnique", "isUnique", "isUnique", "isUnique", "isUnique"]
[[["deer", "door", "cake", "card"]], ["dear"], ["cart"], ["cane"], ["make"], ["cake"]]
输出
[null, false, true, false, true, true]

解释
ValidWordAbbr validWordAbbr = new ValidWordAbbr(["deer", "door", "cake", "card"]);
validWordAbbr.isUnique("dear"); // 返回 false,字典中的 "deer" 与输入 "dear" 的缩写都是 "d2r",但这两个单词不相同
validWordAbbr.isUnique("cart"); // 返回 true,字典中不存在缩写为 "c2t" 的单词
validWordAbbr.isUnique("cane"); // 返回 false,字典中的 "cake" 与输入 "cane" 的缩写都是 "c2e",但这两个单词不相同
validWordAbbr.isUnique("make"); // 返回 true,字典中不存在缩写为 "m2e" 的单词
validWordAbbr.isUnique("cake"); // 返回 true,因为 "cake" 已经存在于字典中,并且字典中没有其他缩写为 "c2e" 的单词

 

提示:

  • 1 <= dictionary.length <= 3 * 104
  • 1 <= dictionary[i].length <= 20
  • dictionary[i] 由小写英文字母组成
  • 1 <= word <= 20
  • word 由小写英文字母组成
  • 最多调用 5000isUnique

解法

哈希表实现,其中 key 存放单词缩写,value 存放单词缩写所对应的所有单词的集合。

Python3

class ValidWordAbbr:
    def __init__(self, dictionary: List[str]):
        self.words = defaultdict(set)
        for word in dictionary:
            abbr = self.word_abbr(word)
            self.words[abbr].add(word)

    def isUnique(self, word: str) -> bool:
        abbr = self.word_abbr(word)
        words = self.words[abbr]
        return not words or (len(words) == 1 and word in words)

    def word_abbr(self, s):
        return s if len(s) < 3 else f'{s[0]}{len(s) - 2}{s[-1]}'


# Your ValidWordAbbr object will be instantiated and called as such:
# obj = ValidWordAbbr(dictionary)
# param_1 = obj.isUnique(word)

Java

class ValidWordAbbr {
    private Map<String, Set<String>> words;

    public ValidWordAbbr(String[] dictionary) {
        words = new HashMap<>();
        for (String word : dictionary) {
            String abbr = abbr(word);
            words.computeIfAbsent(abbr, k -> new HashSet<>()).add(word);
        }
    }

    public boolean isUnique(String word) {
        String abbr = abbr(word);
        Set<String> vals = words.get(abbr);
        return vals == null || (vals.size() == 1 && vals.contains(word));
    }

    private String abbr(String s) {
        int n = s.length();
        return n < 3 ? s : s.charAt(0) + Integer.toString(n - 2) + s.charAt(n - 1);
    }
}

/**
 * Your ValidWordAbbr object will be instantiated and called as such:
 * ValidWordAbbr obj = new ValidWordAbbr(dictionary);
 * boolean param_1 = obj.isUnique(word);
 */

C++

class ValidWordAbbr {
public:
    unordered_map<string, unordered_set<string>> words;

    ValidWordAbbr(vector<string>& dictionary) {
        for (auto word : dictionary) {
            auto abbr = wordAbbr(word);
            words[abbr].insert(word);
        }
    }

    bool isUnique(string word) {
        auto abbr = wordAbbr(word);
        if (!words.count(abbr)) return true;
        auto vals = words[abbr];
        return vals.size() == 1 && vals.count(word);
    }

    string wordAbbr(string s) {
        int n = s.size();
        return n < 3 ? s : s.substr(0, 1) + to_string(n - 2) + s.substr(n - 1, 1);
    }
};

/**
 * Your ValidWordAbbr object will be instantiated and called as such:
 * ValidWordAbbr* obj = new ValidWordAbbr(dictionary);
 * bool param_1 = obj->isUnique(word);
 */

Go

type ValidWordAbbr struct {
	words map[string]map[string]bool
}

func Constructor(dictionary []string) ValidWordAbbr {
	words := make(map[string]map[string]bool)
	for _, word := range dictionary {
		abbr := wordAbbr(word)
		if words[abbr] == nil {
			words[abbr] = make(map[string]bool)
		}
		words[abbr][word] = true
	}
	return ValidWordAbbr{words}
}

func (this *ValidWordAbbr) IsUnique(word string) bool {
	abbr := wordAbbr(word)
	words := this.words[abbr]
	return words == nil || (len(words) == 1 && words[word])
}

func wordAbbr(s string) string {
	n := len(s)
	if n <= 2 {
		return s
	}
	return s[0:1] + strconv.Itoa(n-2) + s[n-1:]
}

/**
 * Your ValidWordAbbr object will be instantiated and called as such:
 * obj := Constructor(dictionary);
 * param_1 := obj.IsUnique(word);
 */

...