Skip to content

Commit 9c0a437

Browse files
authored
Merge branch 'master' into aminos
2 parents 3854f43 + c3b07ae commit 9c0a437

File tree

9 files changed

+943
-14
lines changed

9 files changed

+943
-14
lines changed

DIRECTORY.md

+8
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
* [Subarray Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subarray_sum.cpp)
1212
* [Subset Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subset_sum.cpp)
1313
* [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp)
14+
* [Wildcard Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/wildcard_matching.cpp)
1415

1516
## Bit Manipulation
1617
* [Count Of Set Bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/count_of_set_bits.cpp)
18+
* [Count Of Trailing Ciphers In Factorial N](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp)
1719
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/hamming_distance.cpp)
1820

1921
## Ciphers
@@ -64,6 +66,9 @@
6466
* [Trie Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/trie_tree.cpp)
6567
* [Trie Using Hashmap](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/trie_using_hashmap.cpp)
6668

69+
## Divide And Conquer
70+
* [Karatsuba Algorithm For Fast Multiplication](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/divide_and_conquer/karatsuba_algorithm_for_fast_multiplication.cpp)
71+
6772
## Dynamic Programming
6873
* [0 1 Knapsack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/0_1_knapsack.cpp)
6974
* [Abbreviation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/abbreviation.cpp)
@@ -181,6 +186,7 @@
181186
* [Modular Division](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_division.cpp)
182187
* [Modular Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_exponentiation.cpp)
183188
* [Modular Inverse Fermat Little Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_inverse_fermat_little_theorem.cpp)
189+
* [N Bonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/n_bonacci.cpp)
184190
* [N Choose R](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/n_choose_r.cpp)
185191
* [Ncr Modulo P](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/ncr_modulo_p.cpp)
186192
* [Number Of Positive Divisors](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/number_of_positive_divisors.cpp)
@@ -237,6 +243,7 @@
237243
* [Fast Integer Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_integer_input.cpp)
238244
* [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp)
239245
* [Iterative Tree Traversals](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/iterative_tree_traversals.cpp)
246+
* [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/lru_cache.cpp)
240247
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp)
241248
* [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/palindrome_of_number.cpp)
242249
* [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/paranthesis_matching.cpp)
@@ -292,6 +299,7 @@
292299
* [Counting Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/counting_sort.cpp)
293300
* [Counting Sort String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/counting_sort_string.cpp)
294301
* [Cycle Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/cycle_sort.cpp)
302+
* [Dnf Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/dnf_sort.cpp)
295303
* [Gnome Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/gnome_sort.cpp)
296304
* [Heap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/heap_sort.cpp)
297305
* [Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/insertion_sort.cpp)

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515

1616
## Overview
1717

18-
The repository is a collection of open-source implementation of a variety of algorithms implemented in C++ and licensed under [MIT License](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/LICENSE). The algorithms span a variety of topics from computer science, mathematics and statistics, data science, machine learning, engineering, etc.. The implementations and the associated documentation are meant to provide a learning resource for educators and students. Hence, one may find more than one implementation for the same objective but using a different algorithm strategies and optimizations.
18+
The repository is a collection of open-source implementation of a variety of algorithms implemented in C++ and licensed under [MIT License](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/LICENSE). These algorithms span a variety of topics from computer science, mathematics and statistics, data science, machine learning, engineering, etc.. The implementations and the associated documentation are meant to provide a learning resource for educators and students. Hence, one may find more than one implementation for the same objective but using a different algorithm strategies and optimizations.
1919

2020
## Features
2121

2222
* The repository provides implementations of various algorithms in one of the most fundamental general purpose languages - [C++](https://en.wikipedia.org/wiki/C%2B%2B).
2323
* Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
24-
* Each source code is atomic using [STL classes](https://en.wikipedia.org/wiki/Standard_Template_Library) and _no external libraries_ are required for their compilation and execution. Thus the fundamentals of the algorithms can be studied in much depth.
24+
* Each source code is atomic using [STL classes](https://en.wikipedia.org/wiki/Standard_Template_Library) and _no external libraries_ are required for their compilation and execution. Thus, the fundamentals of the algorithms can be studied in much depth.
2525
* Source codes are [compiled and tested](https://github.com/TheAlgorithms/C-Plus-Plus/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of three major operating systems viz., Windows, MacOS and Ubuntu (Linux) using MSVC 16 2019, AppleClang 11.0 and GNU 7.5.0 respectively.
2626
* Strict adherence to [C++11](https://en.wikipedia.org/wiki/C%2B%2B11) standard ensures portability of code to embedded systems as well like ESP32, ARM Cortex, etc. with little to no changes.
2727
* Self-checks within programs ensure correct implementations with confidence.

backtracking/wildcard_matching.cpp

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/**
2+
* @file
3+
* @brief Implementation of the [Wildcard
4+
* Matching](https://www.geeksforgeeks.org/wildcard-pattern-matching/) problem.
5+
* @details
6+
* Given a matching string and a pattern, implement wildcard pattern
7+
* matching with support for `?` and `*`. `?` matches any single character.
8+
* `*` matches any sequence of characters (including the empty sequence).
9+
* The matching should cover the entire matching string (not partial). The task
10+
* is to determine if the pattern matches with the matching string
11+
* @author [Swastika Gupta](https://github.com/Swastyy)
12+
*/
13+
14+
#include <cassert> /// for assert
15+
#include <iostream> /// for IO operations
16+
#include <vector> /// for std::vector
17+
18+
/**
19+
* @namespace backtracking
20+
* @brief Backtracking algorithms
21+
*/
22+
namespace backtracking {
23+
/**
24+
* @namespace wildcard_matching
25+
* @brief Functions for the [Wildcard
26+
* Matching](https://www.geeksforgeeks.org/wildcard-pattern-matching/) problem.
27+
*/
28+
namespace wildcard_matching {
29+
/**
30+
* @brief The main function implements if pattern can be matched with given
31+
* string
32+
* @param s is the given matching string
33+
* @param p is the given pattern
34+
* @param pos1 is the starting index
35+
* @param pos2 is the last index
36+
* @returns 1 if pattern matches with matching string otherwise 0
37+
*/
38+
std::vector<std::vector<int64_t>> dpTable(1000, std::vector<int64_t>(1000, -1));
39+
bool wildcard_matching(std::string s, std::string p, uint32_t pos1,
40+
uint32_t pos2) {
41+
uint32_t n = s.length();
42+
uint32_t m = p.length();
43+
// matching is successfull if both strings are done
44+
if (pos1 == n && pos2 == m) {
45+
return true;
46+
}
47+
48+
// matching is unsuccessfull if pattern is not finished but matching string
49+
// is
50+
if (pos1 != n && pos2 == m) {
51+
return false;
52+
}
53+
54+
// all the remaining characters of patterns must be * inorder to match with
55+
// finished string
56+
if (pos1 == n && pos2 != m) {
57+
while (pos2 < m && p[pos2] == '*') {
58+
pos2++;
59+
}
60+
61+
return pos2 == m;
62+
}
63+
64+
// if already calculted for these positions
65+
if (dpTable[pos1][pos2] != -1) {
66+
return dpTable[pos1][pos2];
67+
}
68+
69+
// if the characters are same just go ahead in both the string
70+
if (s[pos1] == p[pos2]) {
71+
return dpTable[pos1][pos2] =
72+
wildcard_matching(s, p, pos1 + 1, pos2 + 1);
73+
}
74+
75+
else {
76+
// can only single character
77+
if (p[pos2] == '?') {
78+
return dpTable[pos1][pos2] =
79+
wildcard_matching(s, p, pos1 + 1, pos2 + 1);
80+
}
81+
// have choice either to match one or more charcters
82+
else if (p[pos2] == '*') {
83+
return dpTable[pos1][pos2] =
84+
wildcard_matching(s, p, pos1, pos2 + 1) ||
85+
wildcard_matching(s, p, pos1 + 1, pos2);
86+
}
87+
// not possible to match
88+
else {
89+
return dpTable[pos1][pos2] = 0;
90+
}
91+
}
92+
}
93+
94+
} // namespace wildcard_matching
95+
} // namespace backtracking
96+
97+
/**
98+
* @brief Self-test implementations
99+
* @returns void
100+
*/
101+
static void test() {
102+
// 1st test
103+
std::cout << "1st test ";
104+
std::string matching1 = "baaabab";
105+
std::string pattern1 = "*****ba*****ab";
106+
assert(backtracking::wildcard_matching::wildcard_matching(matching1,
107+
pattern1, 0, 0) ==
108+
1); // here the pattern matches with given string
109+
std::cout << "passed" << std::endl;
110+
111+
// 2nd test
112+
std::cout << "2nd test ";
113+
std::string matching2 = "baaabab";
114+
std::string pattern2 = "ba*****ab";
115+
assert(backtracking::wildcard_matching::wildcard_matching(matching2,
116+
pattern2, 0, 0) ==
117+
1); // here the pattern matches with given string
118+
std::cout << "passed" << std::endl;
119+
120+
// 3rd test
121+
std::cout << "3rd test ";
122+
std::string matching3 = "baaabab";
123+
std::string pattern3 = "ba*ab";
124+
assert(backtracking::wildcard_matching::wildcard_matching(matching3,
125+
pattern3, 0, 0) ==
126+
1); // here the pattern matches with given string
127+
std::cout << "passed" << std::endl;
128+
129+
// 4th test
130+
std::cout << "4th test ";
131+
std::string matching4 = "baaabab";
132+
std::string pattern4 = "a*ab";
133+
assert(backtracking::wildcard_matching::wildcard_matching(matching4,
134+
pattern4, 0, 0) ==
135+
1); // here the pattern matches with given string
136+
std::cout << "passed" << std::endl;
137+
138+
// 5th test
139+
std::cout << "5th test ";
140+
std::string matching5 = "baaabab";
141+
std::string pattern5 = "aa?ab";
142+
assert(backtracking::wildcard_matching::wildcard_matching(matching5,
143+
pattern5, 0, 0) ==
144+
1); // here the pattern matches with given string
145+
std::cout << "passed" << std::endl;
146+
}
147+
148+
/**
149+
* @brief Main function
150+
* @returns 0 on exit
151+
*/
152+
int main() {
153+
test(); // run self-test implementations
154+
return 0;
155+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @file
3+
* @brief [Count the number of
4+
* ciphers](https://www.tutorialspoint.com/count-trailing-zeros-in-factorial-of-a-number-in-cplusplus) in `n!` implementation
5+
* @details
6+
* Given an integer number as input. The goal is to find the number of trailing
7+
zeroes in the factorial calculated for
8+
* that number. A factorial of a number N is a product of all numbers in the
9+
range [1, N].
10+
11+
* We know that we get a trailing zero only if the number is multiple of 10 or
12+
has a factor pair (2,5). In all factorials of
13+
* any number greater than 5, we have many 2s more than 5s in the prime
14+
factorization of that number. Dividing a
15+
* number by powers of 5 will give us the count of 5s in its factors. So, the
16+
number of 5s will tell us the number of trailing zeroes.
17+
* @author [Swastika Gupta](https://github.com/Swastyy)
18+
*/
19+
20+
#include <cassert> /// for assert
21+
#include <iostream> /// for IO operations
22+
23+
/**
24+
* @namespace bit_manipulation
25+
* @brief Bit manipulation algorithms
26+
*/
27+
namespace bit_manipulation {
28+
/**
29+
* @namespace count_of_trailing_ciphers_in_factorial_n
30+
* @brief Functions for the [Count the number of
31+
* ciphers](https://www.tutorialspoint.com/count-trailing-zeros-in-factorial-of-a-number-in-cplusplus)
32+
* in `n!` implementation
33+
*/
34+
namespace count_of_trailing_ciphers_in_factorial_n {
35+
/**
36+
* @brief Function to count the number of the trailing ciphers
37+
* @param n number for which `n!` ciphers are returned
38+
* @return count, Number of ciphers in `n!`.
39+
*/
40+
uint64_t numberOfCiphersInFactorialN(uint64_t n) {
41+
// count is to store the number of 5's in factorial(n)
42+
uint64_t count = 0;
43+
44+
// Keep dividing n by powers of
45+
// 5 and update count
46+
for (uint64_t i = 5; n / i >= 1; i *= 5) {
47+
count += static_cast<uint64_t>(n) / i;
48+
}
49+
50+
return count;
51+
}
52+
} // namespace count_of_trailing_ciphers_in_factorial_n
53+
} // namespace bit_manipulation
54+
55+
/**
56+
* @brief Self-test implementations
57+
* @returns void
58+
*/
59+
static void test() {
60+
// 1st test
61+
std::cout << "1st test ";
62+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
63+
numberOfCiphersInFactorialN(395) == 97);
64+
std::cout << "passed" << std::endl;
65+
66+
// 2nd test
67+
std::cout << "2nd test ";
68+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
69+
numberOfCiphersInFactorialN(977) == 242);
70+
std::cout << "passed" << std::endl;
71+
72+
// 3rd test
73+
std::cout << "3rd test ";
74+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
75+
numberOfCiphersInFactorialN(871) == 215);
76+
std::cout << "passed" << std::endl;
77+
78+
// 4th test
79+
std::cout << "4th test ";
80+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
81+
numberOfCiphersInFactorialN(239) == 57);
82+
std::cout << "passed" << std::endl;
83+
84+
// 5th test
85+
std::cout << "5th test ";
86+
assert(bit_manipulation::count_of_trailing_ciphers_in_factorial_n::
87+
numberOfCiphersInFactorialN(0) == 0);
88+
std::cout << "passed" << std::endl;
89+
}
90+
91+
/**
92+
* @brief Main function
93+
* @returns 0 on exit
94+
*/
95+
int main() {
96+
test(); // run self-test implementations
97+
return 0;
98+
}

0 commit comments

Comments
 (0)