Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int longestCommonSubstring(string &A, string &B) {
- // write your code here
- if(A.length() == 0 || B.length() == 0) {
- return 0;
- }
- int lcs = 0, cs = 0;
- for(unsigned i=0; i<A.length(); i++) {
- for(unsigned j=0; j<B.length(); j++) {
- cs = 0;
- while(i+cs < A.length() && j+cs < B.length() && A[i+cs] == B[j+cs]) {
- cs += 1;
- }
- lcs = max(lcs, cs);
- }
- }
- return lcs;
- }
Add Comment
Please, Sign In to add comment