Download Jar. Libraryless. Click here for Pure Java version (53L/1K).
1 | !7 |
2 | |
3 | p {
|
4 | new TreeSet<S> set; |
5 | set.add("pot");
|
6 | set.add("potash");
|
7 | System.out.println(containsPrefixOf(set, "pot")); |
8 | } |
9 | |
10 | sbool containsPrefixOf(TreeSet<S> set, S s) {
|
11 | if (set == null || s == null) false; |
12 | while true {
|
13 | S key = set.floor(s); |
14 | |
15 | if (key == null) false; // s is in front of whole set => no prefix in there |
16 | int n = lengthOfCommonPrefix(key, s); |
17 | if (n == key.length()) return true; // found! |
18 | s = s.substring(0, n); // shorten and try again |
19 | } |
20 | } |
21 | |
22 | static int lengthOfCommonPrefix(String a, String b) {
|
23 | int i = 0, n = Math.min(a.length(), b.length()); |
24 | while (i < n && a.charAt(i) == b.charAt(i)) |
25 | ++i; |
26 | return i; |
27 | } |
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: | 712 / 1630 |
| Version history: | 1 change(s) |
| Referenced in: | [show references] |