+ Reply to Thread
Results 1 to 2 of 2

Thread: Find longest common substring

  1. #1
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321

    Find longest common substring

    Given two strings, find the longest common substring.

  2. #2
    Harsha Tumkur is offline Junior Member
    Join Date
    May 2011
    Posts
    2
    public static int longestSubstr(String first, String second) {
    if (first == null || second == null || first.length() == 0 || second.length() == 0) {
    return 0;
    }

    int maxLen = 0;
    int fl = first.length();
    int sl = second.length();
    int[][] table = new int[fl][sl];

    for (int i = 0; i < fl; i++) {
    for (int j = 0; j < sl; j++) {
    if (first.charAt(i) == second.charAt(j)) {
    if (i == 0 || j == 0) {
    table[i][j] = 1;
    }
    else {
    table[i][j] = table[i - 1][j - 1] + 1;
    }
    if (table[i][j] > maxLen) {
    maxLen = table[i][j];
    }
    }
    }
    }
    return maxLen;
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find longest repeated substring
    By Surfer in forum Amazon
    Replies: 1
    Last Post: 13th November 2010, 08:51
  2. Find all substring
    By TopGun in forum Google
    Replies: 1
    Last Post: 13th June 2008, 12:14
  3. find Longest palindrome in a string
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 30th May 2008, 15:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts