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

16
LINES

< > BotCompany Repo | #1013590 // commonPrefix

JavaX fragment (include)

1  
static String commonPrefix(String a, String b) {
2  
  int i = 0, nb = b.length(), na = a.length(), n = min(na, nb);
3  
  while (i < n && a.charAt(i) == b.charAt(i))
4  
    ++i;
5  
  return i == nb ? b : substring(a, 0, i);
6  
}
7  
8  
static S commonPrefix(L<S> l) {
9  
  if (empty(l)) ret "";
10  
  S s = first(l);
11  
  for (int i = 1; i < l(l); i++) {
12  
    if (empty(s)) ret s;
13  
    s = commonPrefix(s, l.get(i)); // Yeah, cold be optimized
14  
  }
15  
  ret s;
16  
}

Author comment

Began life as a copy of #2000591

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1013590
Snippet name: commonPrefix
Eternal ID of this version: #1013590/5
Text MD5: 75689c3f636d4396bdaa03ceb32caab9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-28 15:14:48
Source code size: 439 bytes / 16 lines
Pitched / IR pitched: No / No
Views / Downloads: 522 / 508
Version history: 4 change(s)
Referenced in: [show references]