Skip to content

Instantly share code, notes, and snippets.

View sourabh2k15's full-sized avatar
🎯
Focusing

sourabh2k15 sourabh2k15

🎯
Focusing
View GitHub Profile
class Solution {
public:
string reverseString(string s) {
int l = s.length();
for(int i = 0; i < l/2; i++){
char c = s[i];
s[i] = s[l-1-i];
s[l-1-i] = c;
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;
string stripKDigits(string num, int k){
stringstream out;
int i = 0;
@sourabh2k15
sourabh2k15 / 78-Subsets-Iterative.cpp
Last active February 22, 2019 05:33
Leetcode 78-Subsets Iterative solution
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> powerset;
powerset.push_back(vector<int>(0));
// after each iteration of this loop we are left with the powerset of the subset nums[0..i]
for(int i = 0; i < nums.size(); i++){
// append nums[i] to already recorded subsets to form new ones.
@sourabh2k15
sourabh2k15 / 78-Subsets-Backtracking.cpp
Last active February 22, 2018 01:57
Leetcode 78. Subsets solution using backtracking
class Solution {
private:
vector<vector<int>> powerset; // stores all subsets
vector<int> subset; // temporary subset which will be updated as the recursive function executes
public:
vector<vector<int>> subsets(vector<int>& nums) {
backtrack(nums, 0);
return powerset;
}
@sourabh2k15
sourabh2k15 / 567-Permutation_in_String.cpp
Last active January 10, 2018 06:15
Leetcode 567. Permutation in String
class Solution {
public:
bool checkInclusion(string s1, string s2) {
unordered_map<char, int> table;
for(char c : s1){
table[c]++;
}
int begin = 0, end = 0, counter = table.size();
@sourabh2k15
sourabh2k15 / 159-Longest_Substring_with_At_Most_Two_Distinct_Characters.cpp
Last active January 10, 2018 06:14
159. Longest Substring with At Most Two Distinct Characters
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
if(s.length() == 0) return 0;
unordered_map<char, int> table;
int begin = 0, end = 0, len = 0, counter = 0;
while(end < s.length()){
char current = s[end];
@sourabh2k15
sourabh2k15 / 30-Substring_Concatenation_of_All_Words.cpp
Last active January 10, 2018 06:09
Leetcode 30. Substring with Concatenation of All Words
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
unordered_map<string, int> table;
vector<int> ans;
if(words.size() == 0) return ans;
int window_size = 0;
int word_size = words[0].length();
@sourabh2k15
sourabh2k15 / AnagramsInString.cpp
Created January 9, 2018 18:39
Leetcode 438. Find All Anagrams in a String
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
unordered_map<char, int> table;
vector<int> ans;
for(char c : p){
table[c]++;
}
@sourabh2k15
sourabh2k15 / LongestSubstringWithoutRepeatingChars.cpp
Created January 8, 2018 23:10
Leetcode 3. Longest Substring Without Repeating Characters
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> seen;
int begin = 0, end = 0;
int len = 0;
string ans = "";
@sourabh2k15
sourabh2k15 / 76-Minimum_Window_Substring.cpp
Last active March 20, 2022 16:27
Leetcode 76. Minimum Window Substring
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char, int> table;
// initialize frequency table for t
for(char c : t){
table[c]++;
}