Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chap14-2.js : 'haven' and 'havoc' wrong answer #20

Open
tbswang opened this issue Dec 5, 2018 · 0 comments
Open

Chap14-2.js : 'haven' and 'havoc' wrong answer #20

tbswang opened this issue Dec 5, 2018 · 0 comments

Comments

@tbswang
Copy link

tbswang commented Dec 5, 2018

for the first char in a string , it could be equal.

function longestSubString(a, b) {
  var arr = [];
  for (var i = 0; i < a.length; i++) {
    arr[i] = [];
    for (var j = 0; j < b.length; j++) {
      arr[i][j] = 0;
    }
  }

  var max = 0;
  var index = 0;
  for (var i = 0; i < a.length; i++) {
    for (var j = 0; j < b.length; j++) {
      //for boundary element 
      if (i === 0 || j === 0) {
        if (a[i] === b[j]) {
          arr[i][j] = 1;
          max = 1;
        } else {
          arr[i][j] = 0;
        }
      } else {
        if (a[i] === b[j]) {
          arr[i][j] = arr[i - 1][j - 1] + 1;
        } else {
          arr[i][j] = 0;
        }
      }
      if (max < arr[i][j]) { 
        max = arr[i][j]; 
        index = i;
      }
    }
  }

  var str = "";
  if (max == 0) {
    return "";
  }
  else {
    for (var i = index+1 - max; i <= index; ++i) {
      str += a[i];
    }
    return str;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant