LCOV - code coverage report
Current view: top level - src/src - 1143.longest-common-subsequence.cpp (source / functions) Coverage Total Hit
Test: _coverage_report.dat Lines: 100.0 % 20 20
Test Date: 2025-07-24 17:41:04 Functions: 100.0 % 5 5

            Line data    Source code
       1              : // Copyright (c) 2024 The Authors. All rights reserved.
       2              : //
       3              : // Licensed under the Apache License, Version 2.0 (the "License");
       4              : // you may not use this file except in compliance with the License.
       5              : // You may obtain a copy of the License at
       6              : //
       7              : //      https://www.apache.org/licenses/LICENSE-2.0
       8              : //
       9              : // Unless required by applicable law or agreed to in writing, software
      10              : // distributed under the License is distributed on an "AS IS" BASIS,
      11              : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12              : // See the License for the specific language governing permissions and
      13              : // limitations under the License.
      14              : 
      15              : // Authors: liubang (it.liubang@gmail.com)
      16              : 
      17              : #include <gtest/gtest.h>
      18              : 
      19              : #include <string>
      20              : #include <vector>
      21              : 
      22              : namespace {
      23              : class Solution {
      24              : public:
      25            3 :     int longestCommonSubsequence(const std::string& text1, const std::string& text2) {
      26            3 :         int len1 = text1.length();
      27            3 :         int len2 = text2.length();
      28            3 :         std::vector<std::vector<int>> dp(len1 + 1, std::vector<int>(len2 + 1, 0));
      29            3 :         int ret = 0;
      30           14 :         for (int i = 1; i <= len1; ++i) {
      31           44 :             for (int j = 1; j <= len2; ++j) {
      32           33 :                 if (text1[i - 1] == text2[j - 1]) {
      33            6 :                     dp[i][j] = dp[i - 1][j - 1] + 1;
      34              :                 } else {
      35           27 :                     dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
      36              :                 }
      37           33 :                 if (dp[i][j] > ret) {
      38            6 :                     ret = dp[i][j];
      39              :                 }
      40              :             }
      41              :         }
      42            3 :         return ret;
      43            3 :     }
      44              : };
      45              : } // namespace
      46              : 
      47            4 : TEST(Leetcode, longest_common_subsequence) {
      48            1 :     Solution s;
      49            1 :     EXPECT_EQ(3, s.longestCommonSubsequence("abcde", "ace"));
      50            1 :     EXPECT_EQ(3, s.longestCommonSubsequence("abc", "abc"));
      51            1 :     EXPECT_EQ(0, s.longestCommonSubsequence("abc", "def"));
      52            1 : }
        

Generated by: LCOV version 2.0-1