Skip to content

Commit 11d0597

Browse files
committed
Initial commit
1 parent 2c89ab5 commit 11d0597

14 files changed

+234
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
.vscode

.gitmodules

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "extern/googletest"]
2+
path = extern/googletest
3+
url = ../../google/googletest.git
4+
branch = release-1.10.0

CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(CrackingCoding)
3+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
4+
5+
enable_testing()
6+
include(GoogleTest)
7+
include(PackageAddTest)
8+
9+
add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest")
10+
11+
file(GLOB files "src/**/*.cpp")
12+
foreach(file ${files})
13+
get_filename_component(exec_name ${file} NAME_WE)
14+
package_add_test(${exec_name} ${file})
15+
endforeach()

cmake/PackageAddTest.cmake

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
macro(package_add_test TESTNAME)
2+
# create an exectuable in which the tests will be stored
3+
add_executable(${TESTNAME} ${ARGN})
4+
# link the Google test infrastructure, mocking library, and a default main fuction to
5+
# the test executable. Remove gtest_main if writing your own main function.
6+
target_link_libraries(${TESTNAME} gtest gmock gtest_main)
7+
# gtest_discover_tests replaces gtest_add_tests,
8+
# see https://cmake.org/cmake/help/v3.10/module/GoogleTest.html for more options to pass to it
9+
gtest_discover_tests(${TESTNAME}
10+
# set a working directory so your project root so that you can find test data via paths relative to the project root
11+
WORKING_DIRECTORY ${PROJECT_DIR}
12+
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}"
13+
)
14+
set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
15+
endmacro()

extern/googletest

Submodule googletest added at cb44c86

src/arrays_strings/URLify.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// URLify: Write a method to replace all spaces in a string with '%20: You may assume that the string
2+
// has sufficient space at the end to hold the additional characters, and that you are given the "true"
3+
// length of the string. (Note: If implementing in Java, please use a character array so that you can
4+
// perform this operation in place.)
5+
//
6+
// EXAMPLE
7+
// Input: "Mr John Smith "J 13
8+
// Output: "Mr%20J ohn%20Smith"
9+
//
10+
// Hints: #53, #118
11+
12+
int main(int ac, char **av) {
13+
14+
return 0;
15+
}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Check Permutation: Given two strings, write a method to decide if one is a
2+
// permutation of the other.
3+
//
4+
// Hints: #1, #84, #722, #737
5+
6+
#include <algorithm>
7+
#include <iostream>
8+
#include <string>
9+
10+
using namespace std;
11+
12+
// ------------------------------------------------------------------------------------------------
13+
14+
string sort(const string &s) {
15+
string sort_s(s);
16+
sort(sort_s.begin(), sort_s.end());
17+
return sort_s;
18+
}
19+
20+
bool permutation_1(const string &s, const string &t) {
21+
if (s.length() != t.length()) {
22+
return false;
23+
}
24+
return sort(s) == sort(t);
25+
}
26+
27+
// ------------------------------------------------------------------------------------------------
28+
29+
bool permutation_2(const string &s, const string &t) {
30+
if (s.length() != t.length()) {
31+
return false;
32+
}
33+
34+
int letters[128] = {0};
35+
for (const char &c : s) {
36+
letters[c]++;
37+
}
38+
39+
for (int i = 0; i < t.length(); ++i) {
40+
char c = t[i];
41+
letters[c]--;
42+
if (letters[c] < 0) {
43+
return false;
44+
}
45+
}
46+
47+
return true;
48+
}
49+
50+
// ------------------------------------------------------------------------------------------------
51+
52+
#include "gtest/gtest.h"
53+
54+
TEST(PermutationTest, Trivial) {
55+
56+
EXPECT_TRUE(permutation_1("abcd", "dcba"));
57+
EXPECT_FALSE(permutation_1("abcd", "daba"));
58+
59+
EXPECT_TRUE(permutation_2("abcd", "dcba"));
60+
EXPECT_FALSE(permutation_2("abcd", "daba"));
61+
}

src/arrays_strings/is_unique.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Is Unique: Implement an algorithm to determine if a string has all unique
2+
// characters. What if you cannot use additional data structures?
3+
//
4+
// Hints: #44, #117, #132
5+
6+
#include <iostream>
7+
#include <string>
8+
9+
using namespace std;
10+
11+
// ------------------------------------------------------------------------------------------------
12+
13+
bool isUniqueChars_1(const string& str) {
14+
if (str.length() > 128)
15+
return false;
16+
17+
bool char_set[128];
18+
for (int i = 0; i < str.length(); ++i) {
19+
int val = str[i];
20+
if (char_set[val]) {
21+
return false;
22+
}
23+
char_set[val] = true;
24+
}
25+
return true;
26+
}
27+
28+
// ------------------------------------------------------------------------------------------------
29+
30+
bool isUniqueChars_2(const string& str) {
31+
int checker = 0;
32+
for (int i = 0; i < str.length(); ++i) {
33+
int val = str[i] - 'a';
34+
if ((checker & (1 << val)) > 0) {
35+
return false;
36+
}
37+
checker |= (1 << val);
38+
}
39+
return true;
40+
}
41+
42+
// ------------------------------------------------------------------------------------------------
43+
44+
#include "gtest/gtest.h"
45+
46+
TEST(IsUniqueTest, Trivial) {
47+
48+
EXPECT_TRUE(isUniqueChars_1("abcd"));
49+
EXPECT_FALSE(isUniqueChars_1("abcc"));
50+
51+
EXPECT_TRUE(isUniqueChars_2("abcd"));
52+
EXPECT_FALSE(isUniqueChars_2("abcc"));
53+
}

src/arrays_strings/one_away.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// One Away: There are three types of edits that can be performed on strings: insert a character,
2+
// remove a character, or replace a character. Given two strings, write a function to check if they are
3+
// one edit (or zero edits) away.
4+
//
5+
// EXAMPLE
6+
// pale, ple -> true
7+
// pales, pale -> true
8+
// pale, bale -> true
9+
// pale, bake -> false
10+
//
11+
// Hints: #23, #97, #130
12+
13+
int main(int ac, char **av) {
14+
15+
return 0;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation
2+
// is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
3+
//
4+
// EXAMPLE
5+
// Input: Tact Coa
6+
// Output: True (permutations: "taco cat". "atco cta". etc.)
7+
//
8+
// Hints: #106, #121, #134, #136
9+
10+
int main(int ac, char **av) {
11+
12+
return 0;
13+
}

src/arrays_strings/rotate_matrix.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Rotate Matrix: Given an image represented by an NxN matrix, where each pixel in the image is 4
2+
// bytes, write a method to rotate the image by 90 degrees. (an you do this in place?
3+
//
4+
// Hints: #51, #100
5+
6+
int main(int ac, char **av) {
7+
8+
return 0;
9+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// String Compression: Implement a method to perform basic string compression using the counts
2+
// of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. If the
3+
// "compressed" string would not become smaller than the original string, your method should return
4+
// the original string. You can assume the string has only uppercase and lowercase letters (a - z).
5+
//
6+
// Hints: #92, #110
7+
8+
int main(int ac, char **av) {
9+
10+
return 0;
11+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// String Rotation: Assume you have a method isSubst ring which checks if one word is a substring
2+
// of another. Given two strings, 51 and 52, write code to check if 52 is a rotation of 51 using only one
3+
// call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat").
4+
//
5+
// Hints: #34, #88, #104
6+
7+
int main(int ac, char **av) {
8+
9+
return 0;
10+
}

src/arrays_strings/zero_matrix.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and
2+
// column are set to O.
3+
//
4+
// Hints: #17, #74, #102
5+
6+
int main(int ac, char **av) {
7+
8+
return 0;
9+
}

0 commit comments

Comments
 (0)