shortest unique prefix

Given an array of words, find all shortest unique prefixes to represent each word in the given array. Assume that no word is prefix of another.

Input: arr[] = {"zebra", "dog", "duck", "dove"}
Output: dog, dov, du, z
Explanation: dog => dog
             dove = dov 
             duck = du
             z   => zebra 

Input: arr[] =  {"geeksgeeks", "geeksquiz", "geeksforgeeks"};
Output: geeksf, geeksg, geeksq}

思路:用trie来build字典,build的时候count一下每个字符出现的次数,然后对每个string进行查询,第一count为1的node,即可返回shortest prefix了。

public class shortestPrefix {
  private TrieNode root;
  public shortestPrefix() {
    root = new TrieNode();
  }

   public static void main(String[]  args) {
     String strs[] = {"zebra", "dog", "duck", "dove"};
     shortestPrefix p = new shortestPrefix();
     p.findShortestPrefix(strs);
   }

  public void insert(String word) {
    if(word == null || word.length() == 0) return;
    TrieNode curNode = root;
    for(int i=0; i<word.length(); i++) {
      char c = word.charAt(i);
      int index = c-'a';
      if(curNode.array[index] == null){
        TrieNode temp = new TrieNode();
        curNode.array[index] = temp;
        curNode = temp;
        curNode.count = 1;
      } else {
        curNode.array[index].count++;
        curNode = curNode.array[index];
      }
    }
    curNode.isword = true;
  }

  public  void findShortestPrefix(String[] strs) {
    // build trie;
    for(int i=0; i<strs.length; i++) {
      String str = strs[i];
      insert(str);
    }

    // find shortest prefix;
    for(int i=0; i<strs.length;i++){
      String str = strs[i];
      TrieNode curNode = root;
      StringBuilder sb = new StringBuilder();
      for(int j=0; j<str.length(); j++){
        char c = str.charAt(j);
        int index = c-'a';
        if(curNode.array[index].count == 1){
          sb.append(c);
          System.out.println(sb.toString());
          break;
        } else {
          sb.append(c);
          curNode = curNode.array[index];
        }
      }
    }
  }

  public class TrieNode {
    public TrieNode[] array;
    public int count = 0;
    public boolean isword;

    public TrieNode() {
      array = new TrieNode[26];
    }
  }
}

 

spark ******************************************************* Examiner GPL 2012 Copyright (C) 2012 Altran Praxis Limited, Bath, U.K. ******************************************************* DATE : 16-MAR-2025 21:57:32.89 Usage: spark {options} Argument-list Argument-list = Argument {" " Argument } Argument = ( File-spec [Argument-option] ) | Meta-file-spec Argument-option = ( "-listing_file=" file-spec | "-nolisting_file" ) Meta-file-spec = "@"file-spec Options - all may be abbreviated to the shortest unique prefix Input File Options -source_extension=file-type - specifies source file extension (Default .ada) -index_file=file-spec - specifies index file -noindex_file - suppress index file (Default) -warning_file=file-spec - specifies warning control file -nowarning_file - all warnings reported (Default) -target_compiler_data=file-spec - specifies target compiler data This option is now deprecated by -config_file -notarget_compiler_data - suppress target compiler data (Default) -config_file=file-spec - specifies Examiner configuration file -noconfig_file - suppress configuration file (Default) -noswitch - ignore spark.sw file Output File Options -listing_extension=file-type - specifies listing file extension (Default .lst) -report_file=file-spec - specifies report file name (default SPARK.REP) -noreport_file - suppress report file -html[=dir_spec] - Generate HTML listings and report file -output_directory=dir_spec - Generate report, listing, and proof files in specified directory Default is in and below current working directory -nolistings - suppress all listing files -plain_output - No dates, line, or error numbers in outpu
最新发布
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值