Skip to content

Commit

Permalink
add cmake/tests and onboard a edit distance solution with compressed …
Browse files Browse the repository at this point in the history
…memory usage
  • Loading branch information
AchillesMiller committed Jan 1, 2022
1 parent b639b72 commit 1d8f4eb
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
.idea
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)

project(LeetCode VERSION 1.0)

set (CMAKE_CXX_STANDARD 11)

include_directories(PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

enable_testing()
add_subdirectory(tests)
49 changes: 49 additions & 0 deletions Dynamic_Programming/072.Edit-Distance/072.Edit-Distance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,53 @@ class Solution {

return dp[n1][n2];
}

/**
* @brief calculate the minimum edit distance between word1 and word2
* Another implementation of edit distance with compressed memory usage
* @param word1 const std::string & The reference of first string
* @param word2 const std::string & The reference of second string
* @return The edit distance
*/
int minDistance2(const std::string &word1, const std::string &word2)
{
int n1=word1.size();
int n2=word2.size();

// dp[i][j] : edit distance between word1[0, i) and word2[0, j)
// dp[i][j] = std::min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
// ---------- ---------- -------------
// add-j delete-j replace-j
// if word1[i] != word2[j]
// Compressed to
// dp[j]

std::vector<int> dp(n2+1, 0);

for (auto j = 1; j <= n2; ++j)
dp[j] = j; // distance between word1[0, 0) and word2[0, j), j deletions

for (auto i = 1; i <= n1; ++i)
{
int topleft = dp[0];
// reset dp[i]
dp[0] = i; // edit distance = j add operations

for (auto j = 1; j <= n2; ++j)
{
int top = dp[j];

if (word1[i-1] == word2[j-1])
dp[j] = topleft;
else
dp[j] = std::min({dp[j-1], top, topleft}) + 1;
// -------- --- ---------
// delete-j add-j replace-j

topleft = top; // move the top 1 step to the right
}
}

return dp.back();
}
};
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1195,4 +1195,4 @@
[Bit_Manipulation](https://github.com/wisdompeak/LeetCode/tree/master/Template/Bit_manipulation)
[Combination-Number](https://github.com/wisdompeak/LeetCode/tree/master/Template/Combination-Number)
[RB_Tree](https://github.com/wisdompeak/LeetCode/tree/master/Template/RB_Tree)
[CPP_LANG](https://github.com/wisdompeak/LeetCode/tree/master/Template/CPP_LANG)
[CPP_LANG](https://github.com/wisdompeak/LeetCode/tree/master/Template/CPP_LANG)
12 changes: 12 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include(FetchContent)

FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)

FetchContent_MakeAvailable(googletest)

include(GoogleTest)

add_subdirectory(Dynamic_Programming)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "gtest/gtest.h"
#include <climits> // INT_MAX
#include <algorithm> // std::min({})
#include <vector>
#include <string>
using namespace std;

#include <Dynamic_Programming/072.Edit-Distance/072.Edit-Distance.cpp>

TEST(edit_distance_72_test, I)
{
std::string word1 = "horse";
std::string word2 = "ros";

ASSERT_EQ(Solution().minDistance2(word1, word2), 3);
ASSERT_EQ(Solution().minDistance(word1, word2), 3);
}
9 changes: 9 additions & 0 deletions tests/Dynamic_Programming/072.Edit-Distance/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set(BINARY edit_distance_test)

add_executable(${BINARY} 072.Edit-Distance.t.cpp)

target_link_libraries(${BINARY} gtest_main)

include(GoogleTest)

gtest_discover_tests(${BINARY})
1 change: 1 addition & 0 deletions tests/Dynamic_Programming/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(072\.Edit-Distance)

0 comments on commit 1d8f4eb

Please sign in to comment.