├── 060_edit distance.cpp ├── 065_kmp algo.cpp ├── 064_rabin karp algo.cpp ├── 071_boyre mooore algo.cpp ├── 078_valid ip adress.cpp ├── 055_longest recurring subsequence.cpp ├── 068_Count Palindromic Subsequences.cpp ├── 077_longest common subsequence.cpp ├── strings ├── 052_valid string.cpp ├── 050_why strings are immutable in java.cpp ├── 067_count reversals.cpp ├── 073_longest common prefix.cpp ├── 048_string palindrome.cpp ├── 049_find duplicate characters.cpp ├── 047_reverse a string.cpp ├── 056_print all subsequence of a string.cpp ├── 072_roman no to integer.cpp ├── 053_count and say.cpp ├── 051_check string rotation.cpp ├── 084_remove consequtive characters.cpp ├── 061_next permutation.cpp ├── 086_customers cant get acomputer.cpp ├── 058_split binary string.cpp ├── 080_rearrange characters.cpp ├── 076_min swaps for bracket balancing.cpp ├── 062_balanced paranthesis.cpp ├── 074_min no of flips.cpp ├── 057_permutations of a string.cpp ├── 083_smallest window.cpp ├── 066_mobile numeric.cpp ├── 088_isomorphic strings.cpp ├── 075_second most repeted.cpp ├── 063_word break.cpp ├── 082_print anagrams together.cpp ├── 087_min operations-transform.cpp ├── 069_count a no of given strings.cpp └── 081_min char to make palindrome.cpp ├── arrays ├── 032_3 way partitioning.cpp ├── 035_median of the array.cpp ├── 025_given an array of size n, number k.cpp ├── 008_Kadanes algo.cpp ├── 003_ARRAYS_kth max & min.cpp ├── 007_cyclically rotate an arr by 1.cpp ├── 011_Find duplicates in an array.cpp ├── 016_best time to buy and sell stock.cpp ├── 012_merge 2 sorted arrays without extra space.cpp ├── 002_ARRAY_(find min & max eleement).cpp ├── 034_palindrome.cpp ├── 027_array subset of another.cpp ├── 026_maximum profit.cpp ├── 001_REVERSE AN ARRAY.cpp ├── 023_maximum product subarray.cpp ├── 028_triplet sum in array.cpp ├── 021_subarray with 0 sum.cpp ├── 006_union of 2 arrays.cpp ├── 029_trapping rain water.cpp ├── 031_smallest subarray with sum greater.cpp ├── 019_common elements.cpp ├── 004_ARRAYS_SORT without using algo.cpp ├── 014_next permutation.cpp ├── 018_cout pairs with sums.cpp ├── 033_min swaps.cpp ├── 009_minimize d max diff btw heights.cpp ├── 010_Minimum no. of jumps.cpp ├── 013_merge overlaping intervals.cpp ├── 005_ARRAYS_move all negitive to one side.cpp ├── 024_longest consecutive subsequence.cpp ├── 030_chocolate distribution.cpp └── 020_rearrage array.cpp ├── matrix ├── 038_search a 2d matrix.cpp ├── 041_sorted matrix.cpp ├── 044_rotate a matrix by 90.cpp ├── 039_median in a row wise sorted matrix.cpp ├── 040_row with max 1s.cpp ├── 037_siprally traversing a matrix.cpp ├── 045_kth element in matrix.cpp └── 043_find a specific pair in a matrix.cpp ├── 070_search a word in 2d grid.cpp ├── 046_common elements in all rows.cpp ├── LICENSE ├── 054_longest palindrome.cpp ├── 033_min swaps.cpp ├── 022_factorials of large number.cpp ├── README.md └── 015_count inversion.cpp /060_edit distance.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | //dp problem/.... i shall do it later 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /065_kmp algo.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | 5 | 6 | 7 | */ 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /064_rabin karp algo.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | 5 | 6 | 7 | */ 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /071_boyre mooore algo.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | 5 | 6 | 7 | */ 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /078_valid ip adress.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | 5 | idk but its a backtracking techiniqure 6 | 7 | 8 | */ 9 | 10 | 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /055_longest recurring subsequence.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | //longest recurring subsequence 4 | 5 | //ill do this prob when i will start dp problems 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /068_Count Palindromic Subsequences.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | supoose a b a : 5 | ps will be a b a aba: 6 | dp problem 7 | 8 | */ 9 | 10 | 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /077_longest common subsequence.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | 5 | check the last charecters for bothe string if they are equal the recursively call. 6 | 7 | */ 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /strings/052_valid string.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | //no proper link no question only ajeeb problem 3 | 4 | 5 | //https://www.programiz.com/java-programming/examples/check-valid-shuffle-of-strings 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /strings/050_why strings are immutable in java.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | //why strings are immutable in java 3 | 4 | //https://javarevisited.blogspot.com/2010/10/why-string-is-immutable-or-final-in-java.html#axzz6uRRrAuhP 5 | 6 | //check out the link 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /strings/067_count reversals.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | //Given a string S consisting of only opening and closing curly brackets '{' and '}', find out the minimum number of reversals required to convert the string into a balanced expression. 4 | //A reversal means changing '{' to '}' or vice-versa. 5 | 6 | 7 | //already done this prob in parenthesis 8 | //same thing. 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /arrays/032_3 way partitioning.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | //#include 4 | // 5 | //using namespace std; 6 | // 7 | //int main() 8 | //{ 9 | // 10 | // return 0; 11 | //} 12 | 13 | //using quicksort 14 | 15 | vector threeWayPartition(vector v, int a, int b) 16 | { 17 | int start=0,end=v.size-1; 18 | int i=0; 19 | 20 | while(i<=end) 21 | { 22 | if(v[i]b) 26 | swap(v[i],v[end--]); 27 | 28 | else 29 | i++; 30 | } 31 | return v; 32 | } 33 | -------------------------------------------------------------------------------- /matrix/038_search a 2d matrix.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | 4 | //search a 2d matrix 5 | //as it is sorted we can actually6 apply binary search 6 | 7 | //#include 8 | // 9 | //using namespace std; 10 | // 11 | //int main() 12 | //{ 13 | // 14 | // return 0; 15 | //} 16 | 17 | class Solution{ 18 | public: 19 | bool searchMatrix(vector >&v, int t){ 20 | for(int i=0;i<(int)v.size();i++) 21 | { 22 | if(binary_search(v[i].begin(),v[i].end(),t)) 23 | return 1; 24 | } 25 | return 0; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /strings/073_longest common prefix.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | 5 | vertical approach 6 | 7 | */ 8 | 9 | 10 | class Solution { 11 | public: 12 | string longestCommonPrefix(vector &s) { 13 | string res(""); 14 | if(s.size() == 0) return res; 15 | int i = 0; 16 | while(1) { 17 | for(int j = 0; j < s.size(); ++j) { 18 | if(i == s[j].size() || (j > 0 && s[j][i] != s[j-1][i])) 19 | return res; 20 | } 21 | res += s[0][i]; 22 | ++i; 23 | } 24 | } 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /arrays/035_median of the array.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | //Given an array arr[] of N integers, calculate the median 4 | 5 | //#include 6 | // 7 | //using namespace std; 8 | 9 | class Solution 10 | { 11 | public: 12 | public: 13 | 14 | int find_median(vector v) 15 | { 16 | sort(v.begin(),v.end()); //just sort the array 17 | if(v.size()%2==0) //if array size is even 18 | return (v[v.size()/2]+v[v.size()/2-1])/2; //avg of middle values 19 | else 20 | return v[v.size()/2]; //otherwise median will be the middle value 21 | } 22 | }; 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /arrays/025_given an array of size n, number k.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | //author :shreyamalogi 4 | 5 | //Given an array of size n and a number k, find all elements that appear more than n/k times 6 | 7 | //https://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/ 8 | 9 | //ntg to practice here as this will give o(k) 10 | //we can even use structure 11 | 12 | //in leetcode we see majority element n/2 and n/3 times cuz 13 | //cuz those problems can be solved in o(1) space 14 | 15 | #include 16 | 17 | using namespace std; 18 | 19 | int main() 20 | { 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /arrays/008_Kadanes algo.cpp: -------------------------------------------------------------------------------- 1 | //author:shreyamalogi 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int tt; 9 | cin>>tt; 10 | while(tt--) 11 | { 12 | int n; 13 | cin>>n; 14 | 15 | int a[n],i; 16 | for(i=0;i>a[i]; 18 | 19 | int cs=a[0], ms=a[0]; //we need just 2 variables current sum and maximum sum 20 | 21 | for(i=1; i 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n,k; 10 | cin>>n>>k; 11 | 12 | int a[n],i; 13 | for(i=0; i>a[i]; //array input is done 15 | 16 | sort(a,a+n); //in-built sort function ,STL 17 | cout< 6 | 7 | using namespace std; 8 | 9 | 10 | int main() 11 | { 12 | int t; 13 | cin>>t; 14 | while(t--){ 15 | string s; 16 | cin>>s; 17 | int n = s.length(); 18 | 19 | bool palindrome = true; 20 | for(int i=0;i 5 | using namespace std; 6 | void printDups(string s) 7 | { 8 | map count; 9 | for (int i = 0; i < s.length(); i++) { 10 | count[s[i]]++; 11 | } 12 | 13 | for (auto it : count) { 14 | if (it.second > 1) 15 | cout << it.first << ", count = " << it.second 16 | << "\n"; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | string s= "test string"; 23 | printDups(s); 24 | return 0; 25 | 26 | 27 | } 28 | 29 | 30 | 31 | //s, count = 2 32 | //t, count = 3 33 | 34 | -------------------------------------------------------------------------------- /strings/047_reverse a string.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | // to reverse a string 4 | 5 | #include 6 | using namespace std; 7 | 8 | void reverseString(string& s) 9 | { 10 | int n = s.length(); 11 | 12 | // Swap character starting from two 13 | // corners using 2 pointer approach 14 | for (int i=0, j=n-1; i>s; 23 | reverseString(s); 24 | cout << s; 25 | return 0; 26 | } 27 | 28 | //olleh 29 | 30 | 31 | //stl 32 | //reverse(s.begin(),s.end()); 33 | //for(int i=0;i 23 | 24 | using namespace std; 25 | 26 | int main() 27 | { 28 | 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /arrays/007_cyclically rotate an arr by 1.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | //cycling rotate an array by one 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int tt; 12 | cin>>tt; 13 | while(tt--){ 14 | int n; 15 | cin>>n; 16 | 17 | int a[n], i; 18 | for(i=0; i>a[i]; 20 | 21 | int last =a[n-1]; 22 | 23 | for(i=n-2; i>=0; --i) //prev val to next val 24 | { 25 | a[i+1]=a[i]; 26 | } 27 | 28 | a[0]=last; //because a of last should come to first place 29 | 30 | //print 31 | for(i=0; i 10 | 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | string s; 16 | cin>>s; 17 | 18 | int n=s.length(); 19 | int total = 1< 7 | 8 | using namespace std; 9 | 10 | int convert(string s) 11 | { 12 | unordered_map m; 13 | m['I']=1; 14 | m['V']=5; 15 | m['X']=10; 16 | m['L']=50; 17 | m['C']=100; 18 | m['D']=500; 19 | m['M']=1000; 20 | 21 | if(s.length()==1) 22 | return m[s[0]]; 23 | 24 | int i;sum=0; 25 | 26 | for(i=0;i 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int tt; 12 | cin>>tt; 13 | while(tt--) 14 | { 15 | int n; 16 | cin>>n; 17 | int i,j; 18 | int a[n*n],k=0; 19 | 20 | for(i=0;i>a[k++]; 24 | } 25 | } 26 | 27 | sort(a, a+n*n); 28 | for(i=0;i 9 | 10 | using namespace std; 11 | 12 | void solve() 13 | { 14 | int n; 15 | cin>>n; 16 | int a[n]; 17 | for(int i=0;i>a[i]; 18 | 19 | for(int i=0;i1) cout< 10 | 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | string s1,s2; 16 | cin>>s1>>s2; 17 | 18 | int l1 = s1.length(); //s1 ka length and s2 ka length should be equal to get rotations 19 | int l2 = s2.length(); 20 | 21 | if(l1!=l2) cout<<"no"; 22 | else{ 23 | string t= s1+s2; //concatinate both strings to CHECK THE SUBSTRING s2 24 | if(t.find(s2)!=std::string::npos) //find function //check if s2 is presesent or not 25 | cout<<"yes"; 26 | else 27 | cout<<"no"; 28 | } 29 | return 0; 30 | } 31 | 32 | // 33 | //AACD 34 | //ACDA 35 | //yes 36 | //adda 37 | //addsaaa 38 | //no 39 | -------------------------------------------------------------------------------- /arrays/016_best time to buy and sell stock.cpp: -------------------------------------------------------------------------------- 1 | //author:shreyamalogi 2 | 3 | 4 | //FIRST WE NEED TO RECORD THE MINIMUM VALUE WHILE TRAVERSING THROUGH THE ARRAY 5 | 6 | // WE have to get maximum profit so we will compsare our max profit which is initially 0 with maximum available profit that is on that particular day if you sell a stock what will happen. 7 | 8 | // so on that particular day if you brought a stock at some price so what u buy and what u sell, 9 | 10 | // that differece u have to compare with the answer and that answer is the maximum value 11 | 12 | 13 | class Solution { 14 | public: 15 | int maxProfit(vector& v) { 16 | 17 | int i=0,m=0,m1=INT_MAX; 18 | int ans=0; 19 | for(i=0;i 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n,m; //taking lengths of the array 10 | cin>>n>>m; 11 | 12 | int a1[n], a2[m]; 13 | for(int i=0;i>a1[n]; 14 | for(int i=0;i>a2[m]; 15 | 16 | //take another variable j (i will be last,j will be start) 17 | int j; 18 | int i=n-1; 19 | j=0; 20 | 21 | while(i>=0 && ja2[j]) 24 | swap(a1[i],a2[j]); //swap a1[i] with a2[j] 25 | else 26 | break; //if thios condition is not true then we will break out 27 | 28 | i--; 29 | i++; 30 | } 31 | 32 | //sorting both the arrays (a,a+n) 33 | sort(a1, a1+n); 34 | sort(a2, a2+n); 35 | 36 | //we dont have to output anything so its is done 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /matrix/044_rotate a matrix by 90.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | 4 | //transpose and reverse coloumns 5 | //it automatically rotates by 90 degress in clowckwise direction 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int n,m; 13 | cin>>n>>m; 14 | 15 | int a[n][m],i,j,k; //take k also 16 | for(int i=0;i>a[i][j]; 19 | } 20 | } 21 | 22 | //transpose in place 23 | for(i=0;i 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int n; 12 | cin>>n; 13 | 14 | int a[n],i; 15 | for(int i=0; i>a[i]; 17 | 18 | int mn = INT_MAX, mx=INT_MIN; //SET min to INT_MAX and max to INT_MIN 19 | 20 | //minimum 21 | for(i=0; ia[i]) //if minimum is greater than a[i] 24 | mn=a[i]; //then minimum is equal to a[i] 25 | } 26 | //maximum 27 | for(i=0; i 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int tt; 11 | cin>>tt; 12 | while(tt--) 13 | { 14 | int n; 15 | cin>>n; 16 | int a[n],i; 17 | for(i=0;i>a[i]; 18 | 19 | int j=n-1,ans=0; 20 | i=0; 21 | while(i<=j) //2 pointer prob 22 | { 23 | if(a[i]==a[j]) //the corner elements are equal move both the pointers 24 | { 25 | i++; 26 | j--; 27 | } 28 | else if(a[i]>a[j]) 29 | { 30 | j--; 31 | a[j]+=a[j+1]; //merge 32 | ans++; 33 | 34 | } 35 | else 36 | { 37 | i++; 38 | a[i]+=a[i-1]; 39 | ans++; 40 | } 41 | } 42 | cout< 14 | using namespace std; 15 | 16 | void removeduplicate(string s) 17 | { 18 | if(s.length()<2) 19 | cout<>t; 41 | while(t--) 42 | { 43 | string s; 44 | cin>>s; 45 | removeduplicate(s); 46 | } 47 | return 0; 48 | } 49 | 50 | 51 | //1 52 | //aabb 53 | //ab 54 | -------------------------------------------------------------------------------- /arrays/027_array subset of another.cpp: -------------------------------------------------------------------------------- 1 | 2 | //author :shreyamalogi 3 | 4 | //we actually have 4 methods of approach 5 | //but im using hashing unordered map approach cuz its optimized way but this maping i think we acnt use in interviews 6 | 7 | //tc: o(n) 8 | //sc: o(n) 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | int tt; 17 | cin>>tt; 18 | while(tt--){ 19 | int m,n; 20 | cin>>m>>n; 21 | int a[m],b[n]; 22 | for(int i=0;i>a[i]; 23 | for(int i=0;i>b[i]; 24 | 25 | unordered_mapm1; 26 | 27 | for(int i=0;i 8 | 9 | using namespace std; 10 | 11 | //this is the general code for any buying and selling problem with atmost k times 12 | int maxprofit(int a[], int n,int k) 13 | { 14 | if(k>=n/2) 15 | { 16 | int maxp=0,i; 17 | for(i=0;ia[i-1]) 20 | maxp+=a[i]-a[i-1]; 21 | } 22 | return maxp; 23 | } 24 | } 25 | int mx[k+1]={0} 26 | int lp[k+1]; 27 | 28 | for(int i=0;i>n; 42 | int a[n],i; 43 | int k=2; //k will be 2 cuz they are teling twice (in question) 44 | for(i=0;i 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int n; //till how many u wanna print 12 | cin>>n; 13 | 14 | int a[n],i; 15 | for(i=0; i>a[i]; 17 | 18 | i=0; 19 | int j=n-1; //swapING METHODS WILL TAKE TWO METHODS......first pointer will be at index 0 and second pointer will be pointing towards the last index 20 | while(i 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | long long int n; 17 | cin>>n; 18 | long long int a[n],i; 19 | for(i=0; i>a[i]; 20 | 21 | long long int x,y,z, mx=a[0],mn=a[0],res=a[0]; //let us take variables 22 | 23 | for(i=1; i 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | long long int n,sum; 10 | cin>>n>>sum; 11 | long long int a[n],i; 12 | for(i=0;i>a[i]; 14 | 15 | sort(a,a+n); 16 | long long int j,k,f=0; 17 | 18 | for(i=0;i 10 | 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | int t; 16 | cin>>t; 17 | while(t--) 18 | { 19 | int n; 20 | cin>>n; 21 | int a[n]; 22 | for(int i=0;i>a[i]; 24 | 25 | next_permutation(a,a+n); //just use next permutation wala function of stl 26 | 27 | for(int i=0;i 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | int n; 17 | cin>>n; 18 | string s; 19 | cin>>s; 20 | 21 | 22 | int occupied=0; 23 | int visited[26]={0}; //extra space 24 | int didnotget=0; //which will be answer 25 | 26 | for(int i=0;i 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | unordered_map map; 14 | 15 | 16 | int n,m; //rows and coloums 17 | cin>>n>>m; 18 | int a[n][m]; 19 | 20 | int i,j; 21 | for(i=0;i>a[n][m]; 24 | } 25 | } 26 | 27 | 28 | for(j=0;j 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int tt; 12 | cin>>tt; 13 | while(tt--) 14 | { 15 | int n; 16 | cin>>n; 17 | int a[n],sum=0,i; 18 | for(int i=0; i>a[i]; 21 | } 22 | 23 | unordered_map m; 24 | 25 | for(int i=0; i 10 | using namespace std; 11 | 12 | int maxSubStr(string s, int n) 13 | { 14 | int zeroes = 0, ones = 0; // To store the count of 0s and 1s 15 | 16 | // To store the count of maximum 17 | // substrings str can be divided into 18 | int cnt = 0; 19 | for (int i = 0; i < n; i++) { 20 | if (s[i] == '0') { 21 | zeroes++; 22 | } 23 | else { 24 | ones++; 25 | } 26 | if (zeroes == ones) { 27 | cnt++; 28 | } 29 | } 30 | 31 | if (zeroes != ones) { 32 | return -1; 33 | } 34 | 35 | return cnt; 36 | } 37 | 38 | 39 | int main() 40 | { 41 | string s; 42 | cin>>s; 43 | int n = s.length(); 44 | cout << maxSubStr(s, n); 45 | return 0; 46 | } 47 | 48 | //0100110101 49 | 50 | //4 51 | -------------------------------------------------------------------------------- /arrays/006_union of 2 arrays.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | we actually need to considerv a data structure which dont allow duplicates so we take a set 5 | ordered set will increse the time complexity so we are taking unordered set 6 | we can use merge function: tc: o(m+n) 7 | 8 | 9 | */ 10 | 11 | 12 | //find the union and of 2 sorted arrays 13 | 14 | #include 15 | 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | int tt; 21 | cin>>tt; 22 | while(tt--) 23 | { 24 | int n,m; 25 | cin>>n>>m; 26 | int a[n],b[n],i; 27 | 28 | unordered_mapmap; //cuz map contains distinct values which is our ultimate goal for this problem 29 | 30 | for(i=0; i>a[i]; 33 | map[a[i]]++; 34 | } 35 | 36 | for(i=0;i>b[i]; 39 | map[b[i]]++; 40 | } 41 | cout< 4 | 5 | using namespace std; 6 | 7 | //actually this prob has many sol but the best will o(n) tc, o(1) sc 8 | 9 | 10 | int main() 11 | { 12 | int tt; 13 | cin>>tt; 14 | while(tt--) 15 | { 16 | long long int n; 17 | cin>>n; 18 | 19 | long long int a[n],i; 20 | for(i=0;i>a[i]; 21 | 22 | //we need variables her 23 | long long int sum=0, l, r, lmx=0, rmx=0;//let us take sum which will be the answer 24 | l=0; 25 | r=n-1; 26 | 27 | //so it is a 2 pointer approach untill l is less than r 28 | while(l 13 | 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | int n,x; 19 | cin>>n>>x; 20 | 21 | int a[n],i,l=0,sum=0; 22 | for(i=0;i>a[i]; 23 | 24 | int mn=32000; 25 | 26 | for(i=0;ix) 30 | { 31 | mn=min(mn,i-l+1); 32 | sum-=a[l]; 33 | l++; 34 | } 35 | cout< 13 | 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | 19 | int t; 20 | cin>>t; 21 | while(t--) 22 | { 23 | string s; 24 | cin>>s; 25 | 26 | int i,maxfreq=0; 27 | int freq[26]; //hash array 26 alphabets 28 | memset(freq,0,sizeof(freq)); 29 | 30 | for(i=0;i 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int n1,n2,n3; 15 | cin>>n1>>n2>>n3; 16 | 17 | int a[n1],b[n2],c[n3]; 18 | for(int i=0;i>a[i]; 19 | for(int i=0;i>b[i]; 20 | for(int i=0;i>c[i]; 21 | 22 | unordered_map m1,m2,m3; 23 | 24 | for(int i=0;i v; 29 | for(int i=0;i 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | 17 | int t; 18 | cin>>t; 19 | while(t--) 20 | { 21 | long int n; 22 | cin>>n; 23 | string s; 24 | cin>>s; 25 | 26 | long int i,bracket=0; //take bracket variable 27 | long int count=0; 28 | 29 | for(i=0;i 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | int n; 14 | cin>>n; 15 | 16 | int a[n],i; 17 | for(i=0; i>a[i]; 19 | 20 | // we need 3 variables for counting 0 1 and 2 21 | int c0=0, c1=0, c2=0; 22 | 23 | for(i=0; i& v) { 4 | 5 | next_permutation(v.begin(),v.end()); 6 | 7 | } 8 | }; 9 | 10 | 11 | 12 | ////author :shreyamalogi 13 | // 14 | 15 | ////without using STL 16 | //class solution{ 17 | // public: 18 | // void nextPermutation(vector & nums){ 19 | // bool flag = false; //initialize flag variable with false 20 | // int i; 21 | // for(i=nums.size()-1; i>0;i++) //traverse to the vector from right to left 22 | // 23 | // { 24 | // if(nums[i]>nums[i-1]) //if an element is greater than its right neighbour 25 | // { 26 | // flag=true; //then i will say flag is true 27 | // break; //and i will break out 28 | // } 29 | // } 30 | // 31 | // if(flag){ 32 | // int j; 33 | // for(j=nums.size()-1; j>=i; j--){ 34 | // { 35 | // if(nums[j]>nums[i-1]) 36 | // { 37 | // swap(nums[j], nums[i-1]); //swap and break out 38 | // break; 39 | // } 40 | // } 41 | // } 42 | // else 43 | // i=0; //if there is no such element in the if case then make i as 0 44 | // 45 | // sort(nums.begin()+i, nums.end()) //next greater will be a sorted array in ascending order 46 | // } 47 | // }; 48 | -------------------------------------------------------------------------------- /matrix/039_median in a row wise sorted matrix.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | //median in a row wise sorted array 4 | //fundamental thinking behind this algo is that 5 | //the median in a sorted array then it would be 6 | //greater than n/2 elements 7 | 8 | class Solution{ 9 | public: 10 | int median(> &m, int r, int c){ 11 | int mn= INT_MAX; 12 | int mx= INT_MIN; 13 | 14 | for(int i=0;i 9 | 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int n,k; 15 | cin>>n>>k; 16 | 17 | int a[n]; 18 | for(int i=0;i>a[i]; 19 | 20 | int cnt=0; 21 | unordered_mapm; //take an unordered map and store all the arrays in that map 22 | 23 | for(int i=0;i 6 | 7 | using namespace std; 8 | 9 | string pal(string s, int l, int r) 10 | { 11 | if(s=="" || l>r) 12 | return ""; 13 | 14 | while(l>=0 && r>tt; 26 | while(tt--) 27 | { 28 | string s; 29 | cin>>s; 30 | 31 | int n=s.length(); 32 | 33 | //check for odd length and even length palindromes 34 | //and take the longest outta them 35 | //pal func will check for the palindrome around the center 36 | 37 | string longest = s.substr(0,1); 38 | for(int i=0;ilongest.length()) 42 | longest = p1; 43 | 44 | string p2 = pal(s,i,i+1); 45 | if(p2.length()>longest.length()) 46 | longest = p2; 47 | } 48 | cout< 8 | using namespace std; 9 | bool cmp(char b,char c) 10 | { 11 | 12 | if(b=='{' && c=='}') 13 | return true; 14 | else if(b=='[' && c==']') 15 | return true; 16 | else if(b=='(' && c==')') 17 | return true; 18 | return false; 19 | } 20 | bool ispar(string x) 21 | { 22 | stack s; 23 | for(int i=0;i>t; 45 | while(t--) 46 | { 47 | cin>>a; 48 | if(ispar(a)) 49 | cout<<"balanced"< 8 | 9 | using namespace std; 10 | 11 | int ans(int a[], int n, int k){ 12 | //less than or equal to k 13 | int cnt=0; 14 | for(int i=0;ik) 22 | ++bad; 23 | 24 | //initialize ans with bad value of current window 25 | int ans=bad; 26 | for(int i=0,j=cnt;jk) 30 | --bad; 31 | 32 | //increment the cnt of the current window 33 | if(a[j]>k) 34 | ++bad; 35 | 36 | //update ans if count of bad is less in ciurrent window 37 | ans=min(ans,bad); 38 | } 39 | return ans; 40 | } 41 | 42 | int main() 43 | { 44 | int t; 45 | cin>>t; 46 | while(t--) 47 | { 48 | int n; 49 | cin>>n; 50 | int i,a[n],k; 51 | for(i=0;i>a[i]; 52 | cout< 8 | 9 | using namespace std; 10 | 11 | int ans(int a[], int n, int k){ 12 | //less than or equal to k 13 | int cnt=0; 14 | for(int i=0;ik) 22 | ++bad; 23 | 24 | //initialize ans with bad value of current window 25 | int ans=bad; 26 | for(int i=0,j=cnt;jk) 30 | --bad; 31 | 32 | //increment the cnt of the current window 33 | if(a[j]>k) 34 | ++bad; 35 | 36 | //update ans if count of bad is less in ciurrent window 37 | ans=min(ans,bad); 38 | } 39 | return ans; 40 | } 41 | 42 | int main() 43 | { 44 | int t; 45 | cin>>t; 46 | while(t--) 47 | { 48 | int n; 49 | cin>>n; 50 | int i,a[n],k; 51 | for(i=0;i>a[i]; 52 | cout< 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | int t; 17 | cin>>t; 18 | while(t--) 19 | { 20 | string s; 21 | cin>>s; 22 | int n=s.size(); 23 | 24 | int count1=0; //toggle 1 25 | for(int i=0;i 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | // we need n, k and arr 10 | int n; 11 | cin>>n; 12 | int k; 13 | cin>>k; 14 | int a[n],i; 15 | for(i=0; i>a[i]; 17 | 18 | //lets sort to get min and max 19 | 20 | sort(a,a+n); 21 | 22 | int small=a[0]+k; 23 | int big=a[n-1]-k; 24 | 25 | //swap it 26 | if(small>big) 27 | swap(small,big); 28 | 29 | //run a loop from 1 to n-1 30 | for(i=1;i=small || add<=big) 36 | continue; //then do ntg 37 | 38 | else if(big-sub<=add-small) 39 | small=sub; 40 | 41 | else 42 | big=add; 43 | 44 | } 45 | 46 | //print 47 | cout<=k){ 66 | // mx=max(arr[i-1]+k,arr[n-1]-k); 67 | // mn=min(arr[0]+k,arr[i]-k); 68 | // ans=min(ans,mx-mn); 69 | // } 70 | // } 71 | // return ans; 72 | 73 | -------------------------------------------------------------------------------- /arrays/010_Minimum no. of jumps.cpp: -------------------------------------------------------------------------------- 1 | 2 | //author :shreyamalogi 3 | //mimimum no. of jumps 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int tt; 12 | cin>>tt; 13 | while(tt--) 14 | { 15 | int n; 16 | cin>>n; 17 | int a[n],i; 18 | for(i=0; i>a[i]; 20 | 21 | int mx=a[0], step=a[0], jump=1; //initially 3 variables 22 | for(int i=1; i=mx) 34 | { 35 | cout<<-1; 36 | break; 37 | } 38 | 39 | step=mx-i; 40 | } 41 | } 42 | 43 | 44 | if(i!=n) //if i is not equal to n 45 | cout<<-1< 10 | 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | int n,m; 16 | cin>>n>>m; 17 | int a[n][m]; 18 | for(int i=0;i>a[i][j]; 21 | } 22 | } 23 | 24 | int mre =0; //max row element 25 | int j=m-1; 26 | int i=0; 27 | 28 | while(i=0) 29 | { 30 | if(a[i][j]==1){ 31 | mre = i; 32 | j--; 33 | }else 34 | i++; 35 | } 36 | cout<> a, int n, int m,) 63 | // 64 | // int mre =0; //max row element 65 | // int j=m-1; 66 | // int i=0; 67 | // 68 | // while(i=0) 69 | // { 70 | // if(a[i][j]==1){ 71 | // mre = i; 72 | // j--; 73 | // }else 74 | // i++; 75 | // } 76 | // 77 | // return mre; 78 | // } 79 | //}; 80 | -------------------------------------------------------------------------------- /strings/057_permutations of a string.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | //permutations of a given string 4 | //can be done with stl 5 | //but if that is not allowed then 6 | 7 | 8 | 9 | //#include 10 | // 11 | //using namespace std; 12 | // 13 | //int main() 14 | //{ 15 | // int t; 16 | // cin>>t; 17 | // while(t--) 18 | // { 19 | // string s; 20 | // cin>>s; 21 | // int n= s.length(); 22 | // 23 | // sort(s.begin(),s.end()); 24 | // do{ 25 | // cout< 43 | using namespace std; 44 | 45 | void permutation(string s, int l, int r) 46 | { 47 | if(l==r) //base case 48 | cout<>s; 63 | permutation(s,0,s.length()-1); 64 | cout< 7 | 8 | using namespace std; 9 | 10 | 11 | 12 | int main() 13 | { 14 | int n; 15 | cin>>n; 16 | vector> a; //vector of pair 17 | for(int i=0;i>x>>y; 20 | a.push_back({x,y}); //array me push bk 21 | } 22 | //then sort it 23 | 24 | sort(a.begin(),a.end()); 25 | stack> s; //taking a stack in pair wise 26 | s.push({a[0].first,a[0].second}); //1st parameter ko gusadenge 27 | for(int i=1;i 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n,m; 10 | cin>>n>>m; 11 | 12 | int a[n][m]; 13 | for(int i=0;i>a[i][j]; 18 | } 19 | } 20 | 21 | //spiral order print 22 | int row_start=0, row_end=n-1, col_start=0, col_end=m-1; //initializing 23 | 24 | while(row_start<=row_end && col_start<=col_end) 25 | { 26 | //for row start 27 | for(int col = col_start; col<=col_end; col++) 28 | cout<=col_start;col--) 40 | cout<=row_start;row--) 48 | { 49 | cout< 19 | 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | int n; 25 | cin>>n; 26 | 27 | int a[n], i; 28 | for(i=0; i>a[i]; 30 | 31 | int j=0; //take extra pointer j and initialize it with 0 (pivot) 32 | for(i=0; i 12 | 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | long long int n, m=0, cnt=0, m1=0; 18 | cin>>n; 19 | long long int a,i; 20 | long long int h[100005]={0}; //hash array and let all elements be 0 21 | for(i=0;i>a; 24 | h[a]=1; //will be 1 if the particular element is present 25 | m1=max(a,m1); 26 | } 27 | 28 | for(i=0;i<=m1;i++) //scan though the hash array 29 | { 30 | if(h[i]==1) //that means element is present in the array 31 | cnt++; 32 | 33 | else 34 | { 35 | m=max(m,cnt); //if h[i] is not 1 then record the maximum 36 | cnt=0; //and reset count to 0 37 | } 38 | } 39 | m=max(m,cnt); 40 | cout< 14 | using namespace std; 15 | 16 | int main() { 17 | 18 | int t; 19 | cin>>t; 20 | while(t--) 21 | { 22 | string s,s1; 23 | cin>>s>>s1; 24 | long int i,beg=0,end=0,ansstart=0,len=INT_MAX; 25 | 26 | unordered_map m; 27 | 28 | for(i=0;i0) 35 | window--; 36 | 37 | m[s[end]]--; 38 | 39 | end++; 40 | 41 | while(window==0) 42 | { 43 | if(len>end-beg) 44 | { 45 | len=end-beg; 46 | ansstart=beg; 47 | } 48 | 49 | m[s[beg]]++; 50 | if(m[s[beg]]>0) 51 | window++; 52 | 53 | 54 | beg++; 55 | } 56 | 57 | } 58 | if(len!=INT_MAX) 59 | cout< 7 | 8 | using namespace std; 9 | 10 | void multiply(vector &v, int x) 11 | { 12 | int carry=0,mul; //in multiplication table we have carry 13 | for(int i=0; i v; //take vector of integer type 30 | v.push_back(); //push 1 into the vector initially 31 | 32 | for(int i=1;i<=n;i++) 33 | multiply(v,i); //multiply is a function which has 2 parameters vector and current integer 34 | 35 | reverse(v.begin(),v.end()); //we will reverse the vector 36 | 37 | int i=0; 38 | while(v[i]==0) 39 | i++; 40 | 41 | for(int j=i; j>tt; 49 | while(tt--) 50 | { 51 | int n; 52 | cin>>n; 53 | 54 | // cout< 14 | using namespace std; 15 | 16 | // Function which computes the sequence 17 | string printSequence(string arr[], 18 | string input) 19 | { 20 | string output = ""; 21 | 22 | // length of input string 23 | int n = input.length(); 24 | for (int i=0; i 15 | #include 16 | 17 | using namespace std; 18 | #define MAX_CHARS 256 19 | 20 | bool areIsomorphic(string, string); 21 | 22 | 23 | // This function returns true if str1 and str2 are ismorphic 24 | // else returns false 25 | bool areIsomorphic(string s1, string s2) 26 | { 27 | 28 | if(s1.length()!=s2.length()) 29 | return 0; 30 | 31 | bool present[256]={false}; 32 | int map[256]; 33 | memset(map,-1,sizeof(map)); 34 | 35 | for(int i=0;i>t; 61 | string s1,s2; 62 | while (t--) { 63 | cin>>s1; 64 | cin>>s2; 65 | cout< 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | 12 | long long int n; 13 | cin>>n; 14 | long long int a[n],i,j=0,mn=INT_MAX; 15 | for(i=0;i>a[i]; 16 | 17 | sort(a,a+n); 18 | 19 | long long int m; 20 | cin>>m; 21 | 22 | for(i=m-1;i 54 | // 55 | //using namespace std; 56 | // 57 | //int main() 58 | //{ 59 | // int t; 60 | // cin>>t; 61 | // while(t--) 62 | // { 63 | // int n; 64 | // cin>>n; 65 | // int a[n]; 66 | // for(int i=0;i>a[i]; 67 | // 68 | // int m; 69 | // cin>>m; 70 | // 71 | // sort(a,a+n); 72 | // 73 | // int mn=INT_MAX; 74 | // 75 | // for(int i=0;i+m-1 10 | #define MAX 1000 11 | 12 | int mat[MAX][MAX]; 13 | int kthSmallest(int a[MAX][MAX], int n, int k); 14 | 15 | using namespace std; 16 | 17 | 18 | int kthSmallest(int a[MAX][MAX], int n, int k) 19 | { 20 | if(n==0) 21 | return 0; 22 | 23 | 24 | int s=a[0][0]; //search space 25 | int e=a[n-1][n-1]; 26 | 27 | while(smid){ 33 | j--; 34 | } 35 | 36 | cnt+=(j+1); //store in cnt 37 | } 38 | if(cnt>t; 51 | while(t--){ 52 | int n; 53 | cin>>n; 54 | for(int i=0;i>mat[i][j]; 57 | 58 | int r; 59 | cin>>r; 60 | cout< 20 | using namespace std; 21 | string repeat(vector s) 22 | { 23 | 24 | unordered_map occ; 25 | for (int i = 0; i < s.size(); i++) 26 | occ[s[i]]++; 27 | 28 | int first_max = INT_MIN; 29 | int sec_max = INT_MIN; 30 | for (auto it = occ.begin(); it != occ.end(); it++) { 31 | if (it->second > first_max) { 32 | sec_max = first_max; 33 | first_max = it->second; 34 | } 35 | 36 | else if (it->second > sec_max && 37 | it->second != first_max) 38 | sec_max = it->second; 39 | } 40 | for (auto it = occ.begin(); it != occ.end(); it++) 41 | if (it->second == sec_max) 42 | return it->first; 43 | } 44 | int main() 45 | { 46 | string str; 47 | int n; 48 | vector s ; 49 | cout<<"Enter the number of string : "; 50 | cin>>n; 51 | cout<<"Enter the strings : "; 52 | for(int i=0;i>str; 54 | s.push_back(str); 55 | } 56 | cout <<"The second most used word is : "<< repeat(s); 57 | return 0; 58 | } 59 | 60 | 61 | //Enter the number of string : 5 62 | //Enter the strings : shreya nav nav shreya shreya 63 | //The second most used word is : nav 64 | -------------------------------------------------------------------------------- /arrays/020_rearrage array.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | //Rearrange array in alternating positive & negative items with O(1) extra space 4 | 5 | 6 | //approach:take 2 pointers i(at first) and j(at last) 7 | // +ve to first and negitive to last 8 | 9 | // 2 3 -4 -1 6 -9 //i at 2, j at -9, it is barabar so dont do anything 10 | // 2 3 -4 -1 6 -9 //i at 3 (true), j at 6 is false 11 | // 2 3 -4 -1 6 -9 //just move i forward but will be at 6 only 12 | // 2 3 6 -1 -4 -9 //swap then we can see both will point towards -1, as it is -ve j will go to 6 i++,j-- 13 | //then we get condition i<=j then break outaa loop 14 | 15 | //we see that i is pointing towards first -ve element and take k which will be pointing towards 1st positive eleement then swap i with k 16 | //i ko 1 se iterate and k to 2 se iterate 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | int n; 25 | cin>>n; 26 | int a[n]; 27 | for(int i=0;i>a[i]; 28 | 29 | int i=0; int j=n-1; //i ko starting me, j ko last me 30 | while(i<=j) //in this while loop we will get all positive to left and all negitive to right 31 | { 32 | if(a[i]<0 and a[j]>0){ //check false condition 33 | swap(a[i],a[j]); //swap it 34 | i++; 35 | j--; 36 | } 37 | else if(a[i]>0 and a[j]<0){ //check true condition and do ntg just incr/dec 38 | i++; 39 | j--; 40 | } 41 | else if(a[i]>0){ //1 is true just move forward 42 | i++; 43 | } 44 | else if(a[j]<0){ //other 1 is true move bkwards 45 | j--; 46 | } 47 | } 48 | 49 | //checking conditions 50 | if(i==0 or i==n){ 51 | for(int m=0; m 19 | 20 | using namespace std; 21 | 22 | int main() 23 | { 24 | int t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | int n; 29 | cin>>n; 30 | string dict[n]; 31 | 32 | string s; 33 | 34 | 35 | unordered_set st; 36 | for(int i=0;i>dict[i]; //while accepeting the input , insert it into the set 38 | st.insert(dict[i]); 39 | } 40 | 41 | cin>>s; 42 | int n=s.length(); 43 | 44 | //now take a dp array which is gonna be a boolean array only 45 | int dp[n+1]; 46 | memset(dp,0,sizeof(dp)); //let us set all the values of a dp array to 0 47 | dp[n]=1; //set dp of n as 1 because empty sequence is also valid 48 | //let us take twi variables i and j 49 | for(int i=n-1;i>0;i--){ 50 | string word; 51 | for(int j=i; j 12 | #include 13 | using namespace std; 14 | vector > Anagrams(vector& string_list) ; 15 | 16 | vector > Anagrams(vector& s) 17 | { 18 | map, vector> my_map; 19 | 20 | 21 | for(string str : s) 22 | { 23 | 24 | map temp_map; 25 | vector temp_my_list; 26 | for(int i = 0; i < str.length(); ++i) 27 | { 28 | ++temp_map[str[i]]; 29 | } 30 | 31 | 32 | auto it = my_map.find(temp_map); 33 | if (it != my_map.end()) 34 | { 35 | it->second.push_back(str); 36 | } 37 | else 38 | { 39 | temp_my_list.push_back(str); 40 | my_map.insert({ temp_map, temp_my_list }); 41 | } 42 | } 43 | 44 | 45 | vector> result; 46 | 47 | for(auto it = my_map.begin(); 48 | it != my_map.end(); ++it) 49 | { 50 | result.push_back(it->second); 51 | } 52 | 53 | return result; 54 | 55 | } 56 | 57 | 58 | int main() 59 | { 60 | int t; 61 | cin>>t; 62 | while(t--) 63 | { 64 | int n; 65 | cin>>n; 66 | vector string_list(n); 67 | for (int i = 0; i < n; ++i) 68 | cin>>string_list[i]; 69 | vector > result = Anagrams(string_list); 70 | sort(result.begin(),result.end()); 71 | for (int i = 0; i < result.size(); i++) 72 | { 73 | for(int j=0; j < result[i].size(); j++) 74 | { 75 | cout< a and d > b 8 | #include 9 | using namespace std; 10 | #define N 5 11 | 12 | // The function returns maximum value A(c,d) - A(a,b) 13 | // over all choices of indexes such that both c > a 14 | // and d > b. 15 | int findMaxValue(int mat[][N]) 16 | { 17 | //stores maximum value 18 | int maxValue = INT_MIN; 19 | 20 | // maxArr[i][j] stores max of elements in matrix 21 | // from (i, j) to (N-1, N-1) 22 | int maxArr[N][N]; 23 | 24 | // last element of maxArr will be same's as of 25 | // the input matrix 26 | maxArr[N-1][N-1] = mat[N-1][N-1]; 27 | 28 | // preprocess last row 29 | int maxv = mat[N-1][N-1]; // Initialize max 30 | for (int j = N - 2; j >= 0; j--) 31 | { 32 | if (mat[N-1][j] > maxv) 33 | maxv = mat[N - 1][j]; 34 | maxArr[N-1][j] = maxv; 35 | } 36 | 37 | // preprocess last column 38 | maxv = mat[N - 1][N - 1]; // Initialize max 39 | for (int i = N - 2; i >= 0; i--) 40 | { 41 | if (mat[i][N - 1] > maxv) 42 | maxv = mat[i][N - 1]; 43 | maxArr[i][N - 1] = maxv; 44 | } 45 | 46 | // preprocess rest of the matrix from bottom 47 | for (int i = N-2; i >= 0; i--) 48 | { 49 | for (int j = N-2; j >= 0; j--) 50 | { 51 | // Update maxValue 52 | if (maxArr[i+1][j+1] - mat[i][j] > 53 | maxValue) 54 | maxValue = maxArr[i + 1][j + 1] - mat[i][j]; 55 | 56 | // set maxArr (i, j) 57 | maxArr[i][j] = max(mat[i][j], 58 | max(maxArr[i][j + 1], 59 | maxArr[i + 1][j]) ); 60 | } 61 | } 62 | 63 | return maxValue; 64 | } 65 | 66 | 67 | int main() 68 | { 69 | int mat[N][N] = { 70 | { 1, 2, -1, -4, -20 }, 71 | { -8, -3, 4, 2, 1 }, 72 | { 3, 8, 6, 1, 3 }, 73 | { -4, -1, 1, 7, -6 }, 74 | { 0, -4, 10, -5, 1 } 75 | }; 76 | cout << "Maximum Value is " 77 | << findMaxValue(mat); 78 | 79 | return 0; 80 | } 81 | 82 | 83 | //Maximum Value is 18. 84 | -------------------------------------------------------------------------------- /015_count inversion.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | 5 | basically batana hai ki hamara ye jo array hai kitna dur hai isko sort hone se 6 | so ia[j]) //strictly greater 21 | // count++; 22 | // } 23 | // } 24 | // cout< 36 | 37 | using namespace std; 38 | 39 | long long int ans=0; //answer will be our global variable 40 | 41 | void merge(long long int a[], long long int l, long long int m, long long int r)//merge function take 4 parameters 42 | { 43 | int i,j,k; //we need 3 variables 44 | int n1= m-l+1; //as we are splitting array into 2, n1 and n2 45 | int n2= r-m; //will be the sizes of the array 46 | 47 | long long int L[n1], R[n2]; //left subarray with n1 size and right subarray with n2 size 48 | 49 | //run 2 loops 50 | 51 | for(int i=0;i>n; 94 | long long int a[n],i; 95 | for(int i=0;i>a[i]; 96 | 97 | mergesort(a,0,n-1); 98 | 99 | cout< 16 | using namespace std; 17 | 18 | // Function to find minimum number of operations required to transform 19 | // A to B. 20 | int minOps(string& A, string& B) 21 | { 22 | int m = A.length(), n = B.length(); 23 | if (n != m) 24 | return -1; 25 | int count[256]; 26 | memset(count, 0, sizeof(count)); 27 | for (int i=0; i=0; ) 38 | { 39 | // If there is a mismatch, then keep incrementing 40 | // result 'res' until B[j] is not found in A[0..i] 41 | while (i>=0 && A[i] != B[j]) 42 | { 43 | i--; 44 | res++; 45 | } 46 | 47 | // If A[i] and B[j] match 48 | if (i >= 0) 49 | { 50 | i--; 51 | j--; 52 | } 53 | } 54 | return res; 55 | } 56 | 57 | int main() 58 | { 59 | string A = "EACBD"; 60 | string B = "EABCD"; 61 | cout << "Minimum number of operations " 62 | "required is " << minOps(A, B); 63 | return 0; 64 | } 65 | 66 | //Minimum number of operations required is 3 67 | 68 | 69 | 70 | 71 | //#include 72 | // 73 | //using namespace std; 74 | // 75 | //int main() 76 | //{ 77 | // string s1,s2; 78 | // cin>>s1>>s2; 79 | // int i=0,j=0; 80 | // 81 | // int map[256]={0}; 82 | // 83 | // for(int i=0;i0 && j>0) 112 | // { 113 | // if(s1[i]==s2[j]) 114 | // { 115 | // i--; 116 | // j--; 117 | // } 118 | // else 119 | // { 120 | // while(i>0 && s1[i]!=s2[j]) 121 | // { 122 | // i--; 123 | // ans++; 124 | // } 125 | // } 126 | // } 127 | // } 128 | // 129 | // return 0; 130 | //} 131 | -------------------------------------------------------------------------------- /strings/069_count a no of given strings.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | 1- Traverse matrix character by character and take one character as string start 5 | 2- For each character find the string in all the four directions recursively 6 | 3- If a string found, we increase the count 7 | 4- When we are done with one character as start, we repeat the same process for the next character 8 | 5- Calculate the sum of count for each character 9 | 6- Final count will be the answer 10 | 11 | 12 | just a heavy implemenetation problem 13 | 14 | */ 15 | 16 | 17 | #include 18 | using namespace std; 19 | 20 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) 21 | 22 | // utility function to search 23 | // complete string from any 24 | // given index of 2d char array 25 | int internalSearch(string needle, int row, 26 | int col, string hay[], 27 | int row_max, int col_max, int xx) 28 | { 29 | int found = 0; 30 | 31 | if (row >= 0 && row <= row_max && col >= 0 && 32 | col <= col_max && needle[xx] == hay[row][col]) 33 | { 34 | char match = needle[xx]; 35 | xx += 1; 36 | 37 | hay[row][col] = 0; 38 | 39 | if (needle[xx] == 0) 40 | { 41 | found = 1; 42 | } 43 | else 44 | { 45 | 46 | // through Backtrack searching 47 | // in every directions 48 | found += internalSearch(needle, row, 49 | col + 1, hay, 50 | row_max, col_max,xx); 51 | found += internalSearch(needle, row, col - 1, 52 | hay, row_max, col_max,xx); 53 | found += internalSearch(needle, row + 1, col, 54 | hay, row_max, col_max,xx); 55 | found += internalSearch(needle, row - 1, col, 56 | hay, row_max, col_max,xx); 57 | } 58 | hay[row][col] = match; 59 | } 60 | return found; 61 | } 62 | 63 | // Function to search the string in 2d array 64 | int searchString(string needle, int row, int col, 65 | string str[], int row_count, 66 | int col_count) 67 | { 68 | int found = 0; 69 | int r, c; 70 | 71 | for (r = 0; r < row_count; ++r) 72 | { 73 | for (c = 0; c < col_count; ++c) 74 | { 75 | found += internalSearch(needle, r, c, str, 76 | row_count - 1, 77 | col_count - 1, 0); 78 | } 79 | } 80 | return found; 81 | } 82 | 83 | // Driver code 84 | int main() 85 | { 86 | string needle = "MAGIC"; 87 | string input[] = { "BBABBM", 88 | "CBMBBA", 89 | "IBABBG", 90 | "GOZBBI", 91 | "ABBBBC", 92 | "MCIGAM" }; 93 | string str[ARRAY_SIZE(input)]; 94 | int i; 95 | for (i = 0; i < ARRAY_SIZE(input); ++i) 96 | { 97 | str[i] = input[i]; 98 | } 99 | 100 | cout << "count: " << searchString(needle, 0, 0, str, 101 | ARRAY_SIZE(str), 102 | str[0].size()) << endl; 103 | return 0; 104 | } 105 | 106 | //count: 3 107 | 108 | -------------------------------------------------------------------------------- /strings/081_min char to make palindrome.cpp: -------------------------------------------------------------------------------- 1 | //author :shreyamalogi 2 | 3 | /* 4 | Minimum characters to be added at front to make 5 | string palindrome 6 | 7 | brute force 8 | tart checking the string each 9 | time if it is a palindrome and 10 | if not, then delete the last 11 | character and check again. 12 | When the string gets reduced to 13 | wither a palindrome or empty then the number of characters deleted from the end till now will be the answer as those characters could have been inserted at the beginning of the original string in the order which will will 14 | make the string a palindrome. 15 | 16 | o(n2) tc 17 | 18 | but for better approach we can use kmp algo 19 | 20 | */ 21 | 22 | 23 | //#include 24 | //using namespace std; 25 | // 26 | //// function for checking string is palindrome or not 27 | //bool ispalindrome(string s) 28 | //{ 29 | // int l = s.length(); 30 | // int j; 31 | // 32 | // for(int i = 0, j = l - 1; i <= j; i++, j--) 33 | // { 34 | // if(s[i] != s[j]) 35 | // return false; 36 | // } 37 | // return true; 38 | //} 39 | // 40 | // 41 | //int main() 42 | //{ 43 | // string s = "BABABAA"; 44 | // int cnt = 0; 45 | // int flag = 0; 46 | // 47 | // while(s.length()>0) 48 | // { 49 | // // if string becomes palindrome then break 50 | // if(ispalindrome(s)) 51 | // { 52 | // flag = 1; 53 | // break; 54 | // } 55 | // else 56 | // { 57 | // cnt++; 58 | // 59 | // // erase the last element of the string 60 | // s.erase(s.begin() + s.length() - 1); 61 | // } 62 | // } 63 | // 64 | // // print the number of insertion at front 65 | // if(flag) 66 | // cout << cnt; 67 | //} 68 | ////2 69 | 70 | 71 | #include 72 | using namespace std; 73 | 74 | // returns vector lps for given string str 75 | vector computeLPSArray(string str) 76 | { 77 | int M = str.length(); 78 | vector lps(M); 79 | 80 | int len = 0; 81 | lps[0] = 0; // lps[0] is always 0 82 | 83 | // the loop calculates lps[i] for i = 1 to M-1 84 | int i = 1; 85 | while (i < M) 86 | { 87 | if (str[i] == str[len]) 88 | { 89 | len++; 90 | lps[i] = len; 91 | i++; 92 | } 93 | else // (str[i] != str[len]) 94 | { 95 | // This is tricky. Consider the example. 96 | // AAACAAAA and i = 7. The idea is similar 97 | // to search step. 98 | if (len != 0) 99 | { 100 | len = lps[len-1]; 101 | 102 | // Also, note that we do not increment 103 | // i here 104 | } 105 | else // if (len == 0) 106 | { 107 | lps[i] = 0; 108 | i++; 109 | } 110 | } 111 | } 112 | return lps; 113 | } 114 | 115 | // Method returns minimum character to be added at 116 | // front to make string palindrome 117 | int getMinCharToAddedToMakeStringPalin(string str) 118 | { 119 | string revStr = str; 120 | reverse(revStr.begin(), revStr.end()); 121 | 122 | // Get concatenation of string, special character 123 | // and reverse string 124 | string concat = str + "$" + revStr; 125 | 126 | // Get LPS array of this concatenated string 127 | vector lps = computeLPSArray(concat); 128 | 129 | // By subtracting last entry of lps vector from 130 | // string length, we will get our result 131 | return (str.length() - lps.back()); 132 | } 133 | 134 | 135 | int main() 136 | { 137 | string str = "AACECAAAA"; 138 | cout << getMinCharToAddedToMakeStringPalin(str); 139 | return 0; 140 | } 141 | 142 | --------------------------------------------------------------------------------