Skip to content

Commit 3da5e2a

Browse files
Merge branch 'master' into master
2 parents a841b20 + d438f0f commit 3da5e2a

File tree

5 files changed

+498
-66
lines changed

5 files changed

+498
-66
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
* [Kadanes3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kadanes3.cpp)
303303
* [Kelvin To Celsius](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kelvin_to_celsius.cpp)
304304
* [Lfu Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lfu_cache.cpp)
305+
* [Longest Substring Without Repeating Characters](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/longest_substring_without_repeating_characters.cpp)
305306
* [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lru_cache.cpp)
306307
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/matrix_exponentiation.cpp)
307308
* [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/palindrome_of_number.cpp)
@@ -324,6 +325,7 @@
324325
* [Addition Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/addition_rule.cpp)
325326
* [Bayes Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/bayes_theorem.cpp)
326327
* [Binomial Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/binomial_dist.cpp)
328+
* [Exponential Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/exponential_dist.cpp)
327329
* [Geometric Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/geometric_dist.cpp)
328330
* [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/poisson_dist.cpp)
329331
* [Windowed Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/windowed_median.cpp)
@@ -338,6 +340,7 @@
338340
* [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/range_queries/sparse_table.cpp)
339341

340342
## Search
343+
* [Longest Increasing Subsequence Using Binary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/Longest_Increasing_Subsequence_using_binary_search.cpp)
341344
* [Binary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/binary_search.cpp)
342345
* [Exponential Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/exponential_search.cpp)
343346
* [Fibonacci Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/fibonacci_search.cpp)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @file
3+
* @brief Solution for Longest Substring Without Repeating Characters problem.
4+
* @details
5+
* Problem link: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
6+
*
7+
* Intuition:
8+
* 1) The intuition is straightforward and simple. We track the frequency of characters.
9+
* 2) Since we can't use a string to track the longest substring without repeating characters efficiently (as removing a character from the front of a string isn't O(1)), we optimize the solution using a deque approach.
10+
*
11+
* Approach:
12+
* 1) Initialize an unordered_map to track the frequency of characters.
13+
* 2) Use a deque for pushing characters, and update the result deque (`res`) with the current deque (`temp`)
14+
* whenever we find a longer substring.
15+
* 3) Use a while loop to reduce the frequency from the front, incrementing `i`,
16+
* and removing characters from the `temp` deque as we no longer need them.
17+
* 4) Return `res.size()` as we are interested in the length of the longest substring.
18+
*
19+
* Time Complexity: O(N)
20+
* Space Complexity: O(N)
21+
*
22+
* I hope this helps to understand.
23+
* Thank you!
24+
* @author [Ashish Kumar Sahoo](github.com/ashish5kmax)
25+
**/
26+
27+
#include <iostream> // for IO Operations
28+
#include <unordered_map> // for std::unordered_map
29+
#include <deque> // for std::deque
30+
#include <string> // for string class/string datatype which is taken as input
31+
#include <cassert> // for assert
32+
33+
/**
34+
* @class Longest_Substring
35+
* @brief Class that solves the Longest Substring Without Repeating Characters problem.
36+
*/
37+
class Longest_Substring {
38+
public:
39+
/**
40+
* @brief Function to find the length of the longest substring without repeating characters.
41+
* @param s Input string.
42+
* @return Length of the longest substring.
43+
*/
44+
int lengthOfLongestSubstring(std::string s) {
45+
// If the size of string is 1, then it will be the answer.
46+
if (s.size() == 1) return 1;
47+
48+
// Map used to store the character frequency.
49+
std::unordered_map<char, int> m;
50+
int n = s.length();
51+
52+
// Deque to remove from back if repeating characters are present.
53+
std::deque<char> temp;
54+
std::deque<char> res;
55+
int i, j;
56+
57+
// Sliding window approach using two pointers.
58+
for (i = 0, j = 0; i < n && j < n;) {
59+
m[s[j]]++;
60+
61+
// If repeating character found, update result and remove from the front.
62+
if (m[s[j]] > 1) {
63+
if (temp.size() > res.size()) {
64+
res = temp;
65+
}
66+
67+
while (m[s[j]] > 1) {
68+
temp.pop_front();
69+
m[s[i]]--;
70+
i++;
71+
}
72+
}
73+
74+
// Add the current character to the deque.
75+
temp.push_back(s[j]);
76+
j++;
77+
}
78+
79+
// Final check to update result.
80+
if (temp.size() > res.size()) {
81+
res = temp;
82+
}
83+
84+
return res.size(); // Return the length of the longest substring.
85+
}
86+
};
87+
88+
/**
89+
* @brief Self-test implementations
90+
* @returns void
91+
*/
92+
static void tests() {
93+
Longest_Substring soln;
94+
assert(soln.lengthOfLongestSubstring("abcabcbb") == 3);
95+
assert(soln.lengthOfLongestSubstring("bbbbb") == 1);
96+
assert(soln.lengthOfLongestSubstring("pwwkew") == 3);
97+
assert(soln.lengthOfLongestSubstring("") == 0); // Test case for empty string
98+
assert(soln.lengthOfLongestSubstring("abcdef") == 6); // Test case for all unique characters
99+
assert(soln.lengthOfLongestSubstring("a") == 1); // Single character
100+
std::cout << "All test cases passed!" << std::endl;
101+
}
102+
103+
/**
104+
* @brief Main function.
105+
* @return 0 on successful execution.
106+
*/
107+
int main() {
108+
tests(); // run self-test implementations
109+
return 0;
110+
}

probability/exponential_dist.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* @file
3+
* @brief [Exponential
4+
* Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
5+
*
6+
* The exponential distribution is used to model
7+
* events occuring between a Poisson process like radioactive decay.
8+
*
9+
* \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
10+
*
11+
* Summary of variables used:
12+
* \f$\lambda\f$ : rate parameter
13+
*/
14+
15+
#include <cassert> // For assert
16+
#include <cmath> // For std::pow
17+
#include <iostream> // For I/O operation
18+
#include <stdexcept> // For std::invalid_argument
19+
#include <string> // For std::string
20+
21+
/**
22+
* @namespace probability
23+
* @brief Probability algorithms
24+
*/
25+
namespace probability {
26+
/**
27+
* @namespace exponential_dist
28+
* @brief Functions for the [Exponential
29+
* Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
30+
* algorithm implementation
31+
*/
32+
namespace geometric_dist {
33+
/**
34+
* @brief the expected value of the exponential distribution
35+
* @returns \f[\mu = \frac{1}{\lambda}\f]
36+
*/
37+
double exponential_expected(double lambda) {
38+
if (lambda <= 0) {
39+
throw std::invalid_argument("lambda must be greater than 0");
40+
}
41+
return 1 / lambda;
42+
}
43+
44+
/**
45+
* @brief the variance of the exponential distribution
46+
* @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f]
47+
*/
48+
double exponential_var(double lambda) {
49+
if (lambda <= 0) {
50+
throw std::invalid_argument("lambda must be greater than 0");
51+
}
52+
return 1 / pow(lambda, 2);
53+
}
54+
55+
/**
56+
* @brief the standard deviation of the exponential distribution
57+
* @returns \f[\sigma = \frac{1}{\lambda}\f]
58+
*/
59+
double exponential_std(double lambda) {
60+
if (lambda <= 0) {
61+
throw std::invalid_argument("lambda must be greater than 0");
62+
}
63+
return 1 / lambda;
64+
}
65+
} // namespace geometric_dist
66+
} // namespace probability
67+
68+
/**
69+
* @brief Self-test implementations
70+
* @returns void
71+
*/
72+
static void test() {
73+
double lambda_1 = 1;
74+
double expected_1 = 1;
75+
double var_1 = 1;
76+
double std_1 = 1;
77+
78+
double lambda_2 = 2;
79+
double expected_2 = 0.5;
80+
double var_2 = 0.25;
81+
double std_2 = 0.5;
82+
83+
double lambda_3 = 3;
84+
double expected_3 = 0.333333;
85+
double var_3 = 0.111111;
86+
double std_3 = 0.333333;
87+
88+
double lambda_4 = 0; // Test 0
89+
double lambda_5 = -2.3; // Test negative value
90+
91+
const float threshold = 1e-3f;
92+
93+
std::cout << "Test for lambda = 1 \n";
94+
assert(
95+
std::abs(expected_1 - probability::geometric_dist::exponential_expected(
96+
lambda_1)) < threshold);
97+
assert(std::abs(var_1 - probability::geometric_dist::exponential_var(
98+
lambda_1)) < threshold);
99+
assert(std::abs(std_1 - probability::geometric_dist::exponential_std(
100+
lambda_1)) < threshold);
101+
std::cout << "ALL TEST PASSED\n\n";
102+
103+
std::cout << "Test for lambda = 2 \n";
104+
assert(
105+
std::abs(expected_2 - probability::geometric_dist::exponential_expected(
106+
lambda_2)) < threshold);
107+
assert(std::abs(var_2 - probability::geometric_dist::exponential_var(
108+
lambda_2)) < threshold);
109+
assert(std::abs(std_2 - probability::geometric_dist::exponential_std(
110+
lambda_2)) < threshold);
111+
std::cout << "ALL TEST PASSED\n\n";
112+
113+
std::cout << "Test for lambda = 3 \n";
114+
assert(
115+
std::abs(expected_3 - probability::geometric_dist::exponential_expected(
116+
lambda_3)) < threshold);
117+
assert(std::abs(var_3 - probability::geometric_dist::exponential_var(
118+
lambda_3)) < threshold);
119+
assert(std::abs(std_3 - probability::geometric_dist::exponential_std(
120+
lambda_3)) < threshold);
121+
std::cout << "ALL TEST PASSED\n\n";
122+
123+
std::cout << "Test for lambda = 0 \n";
124+
try {
125+
probability::geometric_dist::exponential_expected(lambda_4);
126+
probability::geometric_dist::exponential_var(lambda_4);
127+
probability::geometric_dist::exponential_std(lambda_4);
128+
} catch (std::invalid_argument& err) {
129+
assert(std::string(err.what()) == "lambda must be greater than 0");
130+
}
131+
std::cout << "ALL TEST PASSED\n\n";
132+
133+
std::cout << "Test for lambda = -2.3 \n";
134+
try {
135+
probability::geometric_dist::exponential_expected(lambda_5);
136+
probability::geometric_dist::exponential_var(lambda_5);
137+
probability::geometric_dist::exponential_std(lambda_5);
138+
} catch (std::invalid_argument& err) {
139+
assert(std::string(err.what()) == "lambda must be greater than 0");
140+
}
141+
std::cout << "ALL TEST PASSED\n\n";
142+
}
143+
144+
/**
145+
* @brief Main function
146+
* @return 0 on exit
147+
*/
148+
int main() {
149+
test(); // Self test implementation
150+
return 0;
151+
}

0 commit comments

Comments
 (0)