高级农民
- 积分
- 2978
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-1-21
- 最后登录
- 1970-1-1
|
本帖最后由 Falldawn 于 2024-11-2 19:59 编辑
链接里写得看起来太难受了, ChatGPT的结果,显然不需要再遍历输入了,直接 DFS- class Solution {
- private TrieNode root = new TrieNode();
- // Function to insert a word into the trie
- public void insert(String word) {
- TrieNode node = root;
- for (char ch : word.toCharArray()) {
- node.children.putIfAbsent(ch, new TrieNode());
- node = node.children.get(ch);
- node.count++;
- }
- }
- // Function to find the shortest unique prefix for a word
- public String findPrefix(String word) {
- TrieNode node = root;
- StringBuilder prefix = new StringBuilder();
- for (char ch : word.toCharArray()) {
- prefix.append(ch);
- node = node.children.get(ch);
- if (node.count == 1) {
- return prefix.toString();
- }
- }
- return word; // if no unique prefix, return the word itself
- }
- // Main function to find all shortest unique prefixes for a list of words
- public List<String> shortestUniquePrefixes(String[] words) {
- for (String word : words) {
- insert(word); // Insert each word into the trie
- }
- List<String> result = new ArrayList<>();
- for (String word : words) {
- result.add(findPrefix(word)); // Find and add the unique prefix
- }
- return result;
- }
- }
- class TrieNode {
- Map<Character, TrieNode> children = new HashMap<>();
- int count; // count occurrences of words passing through this node
- }
复制代码- class Solution {
- private TrieNode root = new TrieNode();
- // Function to insert a word into the trie
- public void insert(String word) {
- TrieNode node = root;
- for (char ch : word.toCharArray()) {
- node.children.putIfAbsent(ch, new TrieNode());
- node = node.children.get(ch);
- node.count++;
- }
- }
- // Function to find the shortest unique prefix for a word
- public String findPrefix(String word) {
- TrieNode node = root;
- StringBuilder prefix = new StringBuilder();
- for (char ch : word.toCharArray()) {
- prefix.append(ch);
- node = node.children.get(ch);
- if (node.count == 1) {
- return prefix.toString();
- }
- }
- return word; // if no unique prefix, return the word itself
- }
- public void findUniquePrefixes(TrieNode node, StringBuilder prefix, List<String> result) {
- if (node == null) return;
-
- // If this node is end of a word and count is 1, it's a unique prefix
- if (node.count == 1) {
- result.add(prefix.toString());
- return;
- }
-
- // Recur for all children
- for (Map.Entry<Character, TrieNode> entry : node.children.entrySet()) {
- prefix.append(entry.getKey());
- findUniquePrefixes(entry.getValue(), prefix, result);
- prefix.deleteCharAt(prefix.length() - 1); // Backtrack
- }
- }
- // Main function to find all shortest unique prefixes for a list of words
- public List<String> shortestUniquePrefixes(String[] words) {
- for (String word : words) {
- insert(word); // Insert each word into the trie
- }
- List<String> result = new ArrayList<>();
- StringBuilder sb = new StringBuilder();
- findUniquePrefixes(root, sb, result);
- return result;
- }
- }
- class TrieNode {
- Map<Character, TrieNode> children = new HashMap<>();
- int count; // count occurrences of words passing through this node
- }
复制代码 |
|