Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

27
LINES

< > BotCompany Repo | #1022124 // Look for prefix in TreeSet pure java spike

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (53L/1K).

!7

p {
  new TreeSet<S> set;
  set.add("pot");
  set.add("potash");
  System.out.println(containsPrefixOf(set, "pot"));
}

sbool containsPrefixOf(TreeSet<S> set, S s) {
  if (set == null || s == null) false;
  while true {
    S key = set.floor(s);
    
    if (key == null) false; // s is in front of whole set => no prefix in there
    int n = lengthOfCommonPrefix(key, s);
    if (n == key.length()) return true; // found!
    s = s.substring(0, n); // shorten and try again
  }
}

static int lengthOfCommonPrefix(String a, String b) {
  int i = 0, n = Math.min(a.length(), b.length());
  while (i < n && a.charAt(i) == b.charAt(i))
    ++i;
  return i;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1022124
Snippet name: Look for prefix in TreeSet pure java spike
Eternal ID of this version: #1022124/2
Text MD5: 0acd14dcec17b7efbf09826a5af4ff27
Transpilation MD5: 4bcf7e403c6b79b7bc1344d20f8ee101
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-03-08 00:08:34
Source code size: 687 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 303 / 854
Version history: 1 change(s)
Referenced in: