Skip to content

Commit 8b9d7a5

Browse files
authored
Merge branch 'coder2hacker:main' into main
2 parents 316b975 + d6faaa1 commit 8b9d7a5

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Longest_Substring.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// C++ program to find the length of the longest substring
2+
// without repeating characters
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
// This function returns true if all characters in str[i..j]
6+
// are distinct, otherwise returns false
7+
bool areDistinct(string str, int i, int j)
8+
{
9+
vector<bool> visited(26);
10+
for (int k = i; k <= j; k++) {
11+
if (visited[str[k] - 'a'] == true)
12+
return false;
13+
visited[str[k] - 'a'] = true;
14+
}
15+
return true;
16+
}
17+
18+
// Returns length of the longest substring
19+
// with all distinct characters.
20+
int longestUniqueSubsttr(string str)
21+
{
22+
int n = str.size();
23+
int res = 0; // result
24+
for (int i = 0; i < n; i++)
25+
for (int j = i; j < n; j++)
26+
if (areDistinct(str, i, j))
27+
res = max(res, j - i + 1);
28+
return res;
29+
}
30+
31+
// Driver code
32+
int main()
33+
{
34+
string str = "bobisbobbydeol";
35+
cout << "The input string is " << str << endl;
36+
int len = longestUniqueSubsttr(str);
37+
cout << "The length of the longest non-repeating "
38+
"character substring is "
39+
<< len;
40+
return 0;
41+
}

Trapping_Rain_water

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int trap(vector<int>& height) {
4+
int n = height.size();
5+
vector<int>l(n,height[0]);
6+
vector<int>r(n,height[n-1]);
7+
8+
int lm = height[0];
9+
for(int i = 1;i<n;i++){
10+
lm = max(lm,height[i]);
11+
l[i] = lm;
12+
}
13+
int rm = height[n-1];
14+
for(int i = n-2;i>=0;i--){
15+
rm = max(rm,height[i]);
16+
r[i] = rm;
17+
}
18+
19+
int ans = 0;
20+
for(int i = 0;i<n;i++){
21+
ans += (min(r[i],l[i])-height[i]);
22+
}
23+
24+
return ans;
25+
}
26+
};

0 commit comments

Comments
 (0)