文章目录
session Ⅰ Algorithm
problem Ⅰ
3. Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
Example 1:
Example 2:
Example 3:
Notice that the answer must be a substring, “pwke” is a subsequence and not a substring.
my wrong ans
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int longest=-997, count=0;
unordered_set<char> hashset;
int lens = s.length();
if(!lens)return 0;
for(int i=0; i<lens; i++){
if(hashset.empty() || hashset.find(s[i]) == hashset.end()){
hashset.insert(s[i]);
++count;
}else{
if(count > longest) longest = count;
hashset.clear();
hashset.insert(s[i]);
count = 1;
}
}
if(count > longest)longest = count;
return longest;
}
};
my correct ans
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> hashset;
int ptr = 0, longest = 0;
for(int i=0; i<s.length(); i++){
if(hashset.find(s[i]) != hashset.end()){
if(hashset.size() > longest)longest=hashset.size();
while(hashset.find(s[i]) != hashset.end()){
hashset.erase(s[ptr]);
ptr++;
}
}
hashset.insert(s[i]);
}
if(hashset.size() > longest)longest=hashset.size();
return longest;
}
};
problem Ⅱ
567. Permutation in String
Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
In other words, return true if one of s1’s permutations is the substring of s2.
Example 1:
Example 2:
class Solution {
public:
bool match_(char map1[], char map2[]){
for(int i=0; i<26; i++)
if(map1[i] != map2[i])return false;
return true;
}
bool checkInclusion(string s1, string s2){
char map1[26] = {0};
char map2[26] = {0};
int len1 = s1.length();
int len2 = s2.length();
if(len1 > len2)return false;
for(int i=0; i<len1; i++){
map1[s1[i] - 'a']++;
map2[s2[i] - 'a']++;
}
for(int i=len1; i<len2; i++){
if(match_(map1, map2))return true;
map2[s2[i]-'a']++;
map2[s2[i-len1]-'a']--;
}
if(match_(map1, map2))return true;
return false;
}
};
Window Sliding Technique
this technique is that sometime a nested loop can be converted into a single for loop to reduce the time complexity
let’s start with a problem to illustrate where we can apply this technique
problem:
Given an array of integers of size ‘n’.
Our aim is to calculate the maximum sum of ‘k’
consecutive elements in the array.
Input : arr[] = {100, 200, 300, 400}
k = 2
Output : 700
Input : arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20}
k = 4
Output : 39
We get maximum sum by adding subarray {4, 2, 10, 23}
of size 4.
Input : arr[] = {2, 3}
k = 3
Output : Invalid
There is no subarray of size 3 as size of whole
array is 2.
solution 1 : nested loop
time complexity: O(n*k)
#include <bits/stdc++.h>
using namespace std;
int maxSum(int arr[], int n, int k)
{
int max_sum = INT_MIN;
for (int i = 0; i < n - k + 1; i++) {
int current_sum = 0;
for (int j = 0; j < k; j++)
current_sum = current_sum + arr[i + j];
max_sum = max(current_sum, max_sum);
}
return max_sum;
}
int main()
{
int arr[] = { 1, 4, 2, 10, 2, 3, 1, 0, 20 };
int k = 4;
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxSum(arr, n, k);
return 0;
}
//Output: 24
solution 2: window sliding
time complexity: O(n)
#include <iostream>
using namespace std;
int maxSum(int arr[], int n, int k)
{
if (n < k) {
cout << "Invalid";
return -1;
}
int max_sum = 0;
for (int i = 0; i < k; i++)
max_sum += arr[i];
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
int window_sum = max_sum;
for (int i = k; i < n; i++) {
window_sum += arr[i] - arr[i - k];
max_sum = max(max_sum, window_sum);
}
return max_sum;
}
int main()
{
int arr[] = { 1, 4, 2, 10, 2, 3, 1, 0, 20 };
int k = 4;
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxSum(arr, n, k);
return 0;
}
//Output: 24
O(n*k)
O(n)