Leetcode 527. Word Abbreviation

Problem:

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below.

  1. Begin with the first character and then the number of characters abbreviated, which followed by the last character.
  2. If there are any conflict, that is more than one words share the same abbreviation, a longer prefix is used instead of only the first character until making the map from word to abbreviation become unique. In other words, a final abbreviation cannot map to more than one original words.
  3. If the abbreviation doesn‘t make the word shorter, then keep it as original.

Example:

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

Note:

  1. Both n and the length of each word will not exceed 400.
  2. The length of each word is greater than 1.
  3. The words consist of lowercase English letters only.
  4. The return answers should be in the same order as the original array.

Solution:

  这道题是要我们为输入字符串中的每个单词找到最短的缩写且不存在冲突,这里要注意下,比如intension和intrusion两个单词,i7n,in6n,int5n都存在两个单词与之对应,因此这题的缩写的含义是要找一个最短的缩写,使得不存在多个单词与之对应,所以这三种缩写形式都无法使用。这道题我们定义一个abbreviate函数,k代表缩写字符串中数字之前的字符个数,比如in6n对应的k等于2。pre数组用于存储前缀的长度信息,初始化为1。首先对于所有字符串先进行一个缩写,然后找出所有出现多次的字符串,增加其前缀的长度重新进行缩写,指到所有缩写都不存在冲突为止。

Code:

 1 class Solution {
 2 public:
 3     string abbreviate(string &str,int k){
 4         if(str.size()-2 <= k)
 5             return str;
 6         return str.substr(0,k)+to_string(str.size()-1-k)+str.back();
 7     }
 8     vector<string> wordsAbbreviation(vector<string>& dict) {
 9         int m = dict.size();
10         vector<string> result(m);
11         vector<int> pre(m,1);
12         for(int i = 0;i != m;++i){
13             result[i] = abbreviate(dict[i],pre[i]);
14         }
15         for(int i = 0;i != m;++i){
16             while(true){
17                 unordered_set<int> us;
18                 for(int j = i+1;j != m;++j){
19                     if(result[i] == result[j])
20                         us.insert(j);
21                 }
22                 if(us.empty()) break;
23                 us.insert(i);
24                 for(auto index:us)
25                     result[index] = abbreviate(dict[index],++pre[index]);
26             }
27         }
28         return result;
29     }
30 };

原文地址:https://www.cnblogs.com/haoweizh/p/10261986.html

时间: 2024-07-31 11:02:10

Leetcode 527. Word Abbreviation的相关文章

[LeetCode] 527. Word Abbreviation 单词缩写

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then the number of characters abbreviated, which followed by the last charact

[LeetCode] Valid Word Abbreviation 验证单词缩写

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

LeetCode Valid Word Abbreviation

原题链接在这里:https://leetcode.com/problems/valid-word-abbreviation/#/description 题目: Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the followi

Leetcode: Unique Word Abbreviation

An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations: a) it --> it (no abbreviation) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --&

LeetCode:Word Pattern - 字符串模式匹配

1.题目名称 Word Pattern(字符串模式匹配) 2.题目地址 https://leetcode.com/problems/word-pattern/ 3.题目内容 英文:Given a pattern and a string str, find if str follows the same pattern. 中文:给出一组模式(pattern)和一个字符串(str),查看字符串是否与模式匹配 例如: pattern = "abba",str = "dog cat

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

LeetCode OJ - Word Ladder 2

我发现在leetcode上做题,当我出现TLE问题时,往往是代码有漏洞,有些条件没有考虑到,这道题又验证了我这一想法. 这道题是在上一道的基础上进一步把所有可能得转换序列给出. 同样的先是BFS,与此同时需要一个hashMap记录下每个节点,和他所有父节点的对应关系,然后通过DFS,回溯所有可能的路径. 下面是AC代码. 1 /** 2 * Given two words (start and end), and a dictionary, find all shortest transform

[LeetCode OJ] Word Search 深度优先搜索DFS

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us