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

19
LINES

< > BotCompany Repo | #1012478 // longestCommonSubstring

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3808L/22K).

static S longestCommonSubstring(S s1, S s2) {
  int Start = 0;
  int Max = 0;
  int l1 = l(s1), l2 = l(s2);
  for ping (int i = 0; i < l1; i++) {
    for ping (int j = 0; j < l2; j++) {
      int x = 0;
      while (s1.charAt(i + x) == s2.charAt(j + x)) {
        x++;
        if ((i + x) >= l1 || (j + x) >= l2) break;
      }
      if (x > Max) {
        Max = x;
        Start = i;
      }
    }
  }
  ret s1.substring(Start, Start + Max);
}

Author comment

From https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring#Java

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: #1012478
Snippet name: longestCommonSubstring
Eternal ID of this version: #1012478/5
Text MD5: 8cbedafee24d1a6f476441c60fd15088
Transpilation MD5: 66c6814f49a132e7a4b7a4c68ea41e05
Author: stefan
Category: javax / strings
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-06 11:28:15
Source code size: 462 bytes / 19 lines
Pitched / IR pitched: No / No
Views / Downloads: 335 / 377
Version history: 4 change(s)
Referenced in: [show references]