Leetcode: 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").
Refer to https://discuss.leetcode.com/topic/61799/java-bit-mask-dfs-with-pruning
bit mask refer to http://bookshadow.com/weblog/2016/10/02/leetcode-minimum-unique-word-abbreviation/
1. The key idea of my solution is to preprocess the dictionary to transfer all the words to bit sequences (int):
比如apple 和 amper 字母相同设1,不同设0,所以得到10100
又比如target='apple',word='amble',则word的bitmask为10011
在这个过程中ignore与target长度不一样的,得到每个字典里面的bitmask,下面称呼它为mask,所以mask里为1的位表示与target字母相同
a p p l e
a m b l e
1 0 0 1 1
2. 开始缩写target单词,只不过我们不直接缩写成十进制数,而是先缩写成二进制,1表示保留字母,0表示替换为数字。下面称呼这个当前的缩写结果为curResult, 因为curResult由target缩写而来,所以curResult里为1的位与target字母相同
例如单词manipulation的缩写m2ip6n可以转化为100110000001
m a n i p u l a t i o n
m 2 i p 6 n
1 0 0 1 1 0 0 0 0 0 0 1
3. mask里为1的位表示与target字母相同, 同时curResult里为1的位也与target字母相同, 如果
curResult & mask == curResult,
则说明curResult里为1的位子上,mask也一定是1,那么curResult也一定是mask所对应的那个string的一个缩写,所以这里conflict出现了,所以这个curResult要被skip掉
4. 在所有没有conflict的curResult里面,找到长度最短的一个,如何找到长度最短,可以在recursion里面维护一个当前curResult长度大小的变量,同时有一个变量保存最小长度用以更新
5. 最后将长度最短那个curResult(它目前只是一个二进制缩写),复原成十进制缩写
这是我做过最难的Bitmask的题了
public class Solution {
private int minLen;
private int result; public String minAbbreviation(String target, String[] dictionary) {
// only keep words whose length == target in new dict, then compute their bit masks
Set<Integer> maskSet = new HashSet<>();
for(String s: dictionary){
if(target.length() == s.length()){
maskSet.add(getBitMask(s,target));
}
} // dfs with pruning
minLen = target.length()+1;
result = -1; dfs(target,maskSet,0,0,0); if(minLen > target.length()){
return "";
} // convert result to word
int zeroCnt = 0;
String res = "";
for (int i = target.length()-1; i>=0; i--) {
//遇到0要累加连续零个数,遇到1填原char
int digit = (result & 1);
if(digit == 0){
++zeroCnt;
} else {
if(zeroCnt > 0){
res = zeroCnt + res;
zeroCnt =0;
}
res = target.charAt(i) + res;
}
result >>= 1;
}
if(zeroCnt > 0) res = zeroCnt + res;
return res;
} /**
*
* @param target
* @param maskSet masks of words in dict
* @param start idx at target
* @param curLen current abbr's length
*/
private void dfs(String target,Set<Integer> maskSet,int start,int curLen,int curResult){
// pruning, no need to continue, already not min length
if(curLen >= minLen) return; if(start == target.length()){
// check whether curResult mask conflicts with words in dict
for(int mask:maskSet){
/**
* 单词manipulation的缩写m2ip6n可以转化为100110000001
* m a n i p u l a t i o n
m 2 i p 6 n
1 0 0 1 1 0 0 0 0 0 0 1
* 0代表随意不care,如果这个mask和dict中某个mask的所有1重合代表在意的位置完全相同,
* 说明这个mask和dict中那个词冲突
* 我们要找的是不冲突的mask
*/
if((curResult & mask) == curResult){
return; // conflict
}
}
// no conflict happens, can use
if(minLen > curLen){
minLen = curLen;
result = curResult;
}
return;
} // case 1: replace chars from start in target with number
for (int i = start; i < target.length(); i++) {
//被replace掉的char位置由0代替所以是curResult<<(i+1-start),没replace掉的这里不管,我们只管到i,之后的由backtrack内决定
//注意:不允许word => w11d这种用数字代替但含义不同
if(curLen == 0 || (curResult &1) == 1){
//后者即上一次是保留了字母
dfs(target,maskSet,i+1,curLen+1,curResult<<(i+1-start));
}
} // case 2: no replace from start (curResult << 1)+1代表新的这位保留了char,所以是加一
dfs(target,maskSet,start+1,curLen+1,(curResult << 1)+1);
} // 比如apple 和 amper 字母相同设1,不同设0,所以得到10100
private int getBitMask(String s1,String s2){
int mask = 0;
for (int i = 0; i < s1.length(); i++) {
mask <<= 1;
if(s1.charAt(i) == s2.charAt(i)){
mask += 1;
}
}
return mask;
}
}
Leetcode: Minimum Unique Word Abbreviation的更多相关文章
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 411. Minimum Unique Word Abbreviation
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 288. Unique Word Abbreviation
题目: An abbreviation of a word follows the form <first letter><number><last letter> ...
- Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- Unique Word Abbreviation -- LeetCode
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
随机推荐
- 关于TFS地址改变后,项目迁移的问题。
经常遇到TFS的服务器地址改变,以至于项目全部不能用,如果全部重新打开,然后重新映射,是件很费时.费事的事.但其实是有简单方法的. 找到解决方法文件,即SLN文件. 用记事本打开,找到SccTeamF ...
- 【转】oracle 中随机取一条记录的两种方法
oracle 中随机取一条记录的两种方法 V_COUNT INT:=0; V_NUM INT :=0; 1:TBL_MYTABLE 表中要有一个值连续且唯一的列FID BEGIN SELECT COU ...
- 【Cocos2d-x游戏开发】Cocos2d-x中的数据存储技术
一.引言 数据存储和网络功能可以说是一款游戏中必不可少的功能,如果一款游戏不能保存进度那么它的可玩性必然大打折扣(试想一下,玩家辛辛苦苦玩了一整天的游戏,结果退出时告诉人家不能保存关卡信息,你明天还得 ...
- web项目绝对路径与相对路径的问题
1.绝对路径:就是一个文件url的全部或者磁盘完整的物理地址;例如 http://localhost:8080/navigation/a.jsp就是a.jsp的绝对路径,再例如 D:\TC\a.jsp ...
- Leetcode Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- mysql like 贪婪匹配 同时匹配多个值
LIKE "%a%b%c%",这样匹配出的就是包含a,b,c三个关键词的记录 (三个关键词不在一起时) 不好用 mysql> select count(1) from dm ...
- Windows OS上安装运行Apache Kafka教程
Windows OS上安装运行Apache Kafka教程 下面是分步指南,教你如何在Windows OS上安装运行Apache Zookeeper和Apache Kafka. 简介 本文讲述了如何在 ...
- [转][Starling] 神器——原生Swf一键导出到Starling!
Swf一键导出到Starling中的工具,在Starling使用原生的MovieClip 来自:http://zmliu.github.io/2013/11/09/StarlingSwfTool/ 如 ...
- 星外Xday提权
在星外不是安全模式的时候 但是又没有执行目录 有时候远程调用也不行对吧 winrar 有执行权限的时候C:\windows\IIS Temporary Compressed Files\ 这个目录有 ...
- 野路子出身PowerShell 文件操作实用功能 MSSQL123
因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职, 索性就网上各种搜,各种Demo,各种修改,花了半天时间,最后还是拼凑出来能达到效果了. 本身对PowerShe ...