Check If a String Contains All Binary Codes of Size K | Leetcode 1461

preview_player
Показать описание

1) 0:00 Explaining the problem out loud
2) 1:10 Brute Force
3) 3:10 Optimised Approach
4) 7:15 Coding it up
5) 9:00 Complexity analysis

Рекомендации по теме
Комментарии
Автор

The index in the array starts from 0. We need to create substrings from [0 index to n - k +1) or [0, n-k]. In this case k = 2 so it becomes n-2+1, which is n -1.

CodeWithSunchitDudeja
Автор

You can use sliding window here the TC will be reduced to O(Length of input string) -> Most Optimised
Code ->
bool hasAllCodes(string s, int k) {
int totalCodes = (1 << k);
int i;
set<string>st;
string temp;
int len= s.length();
int val = min(k, len);
for(i = 0; i < val; i++) {
temp += s[i];
}
st.insert(temp);
for(; i < s.length(); i++) {
temp.erase(0, 1);
temp += s[i];
st.insert(temp);
}
if(st.size() >= totalCodes) return true;
return false;
}

atharvakulkarni
Автор

Actually you should explain more optimised code i.e using rolling hash.

OMPRAKASHKUMAR-zyqs
Автор

I am trying to understand how you transverse the string O(n-k) times. From the arrows on the string in your example, you transverse every element 7 times and this is O(n-1) times. Please can you explain

johnademola