博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 411: Minimum Unique Word Abbreviation
阅读量:5893 次
发布时间:2019-06-19

本文共 2586 字,大约阅读时间需要 8 分钟。

Note: This solution is combined with Trie + Generialized Abbreviation:

1. Since it looks for smallest one, we need persist a string to find minimum length.

2. Lots of mistakes:

                                index not moving (index++)

                                index is messed up. (s.charAt(index) - 'a')

                                data is not intialized.

class Solution {    class TrieNode {        TrieNode[] children = new TrieNode[26];        boolean isWord = false;    }        private TrieNode root;    private List
result; private String minL = ""; public String minAbbreviation(String target, String[] dictionary) { if (dictionary.length == 0) return String.valueOf(target.length()); root = new TrieNode(); result = new ArrayList<>(); for (String s : dictionary) addWord(s); generateComb(target, "", 0, 0); for (String s : result) { if (!search(s, root, 0, 0) && (minL.length() == 0 || s.length() < minL.length())) { minL = s; } } return minL; } private void addWord(String s) { TrieNode current = root; for (int i = 0; i < s.length(); i++) { if (current.children[s.charAt(i) - 'a'] == null) { current.children[s.charAt(i) - 'a'] = new TrieNode(); } current = current.children[s.charAt(i) - 'a']; } current.isWord = true; } private boolean search(String s, TrieNode root, int index, int num) { if (root == null) return false; if (num > 0) { for (int i = 0; i < 26; i++) { if (search(s, root.children[i], index, num - 1)) return true; } return false; } if (index == s.length()) return root.isWord; if (Character.isDigit(s.charAt(index))) { int current = 0; while (index < s.length() && Character.isDigit(s.charAt(index))) { current = current * 10 + (s.charAt(index++) - '0'); } return search(s, root, index, current); } return search(s, root.children[s.charAt(index) - 'a'], index + 1, num); } private void generateComb(String word, String current, int count, int index) { if (index == word.length()) { if (count > 0) { current += String.valueOf(count); } result.add(current); return; } generateComb(word, current, count + 1, index + 1); generateComb(word, current + (count > 0 ? count : "") + word.charAt(index), 0, index + 1); }}

 

转载于:https://www.cnblogs.com/shuashuashua/p/7662213.html

你可能感兴趣的文章
div+css实现window xp桌面图标布局(至上而下从左往右)
查看>>
0-1 背包问题
查看>>
运行Maven是报错:No goals have been specified for this build
查看>>
Haskell 差点儿无痛苦上手指南
查看>>
[MODx] Build a CMP (Custom manager page) using MIGX in MODX 2.3 -- 1
查看>>
NTP 服务器配置
查看>>
jQuery自动完成点击html元素
查看>>
[算法]基于分区最近点算法的二维平面
查看>>
linux在文件打包和压缩
查看>>
Angular - - ngList、ngRepeat、ngModelOptions
查看>>
[LeetCode136]Single Number寻找一个数组里只出现一次的数
查看>>
webpack多页应用架构系列(七):开发环境、生产环境傻傻分不清楚?
查看>>
bootstrap - image
查看>>
spring-boot 和 webpack-dev-server联合开发
查看>>
从TimSort说起
查看>>
构建 iOS 界面:子类化 Views
查看>>
笨办法学C 练习1:启用编译器
查看>>
用Golang写一个搜索引擎(0x01)--- 基本概念
查看>>
【算法之美】logn 时间复杂度求解两个有序数组的中位数
查看>>
树的总结--树的性质(树的深度) leetcode
查看>>