├── Add Binary.py ├── Add Digits.cpp ├── Add Strings.cpp ├── Add Two Integers.cpp ├── Arranging Coins.cpp ├── Array Partition.cpp ├── Average Salary Excluding the Minimum and Maximum Salary.cpp ├── Base 7.cpp ├── Build Array from Permutation.cpp ├── Cells in a Range on an Excel Sheet.cpp ├── Check If All 1's Are at Least Length K Places Away.cpp ├── Check If N and Its Double Exist.cpp ├── Check If String Is a Prefix of Array.cpp ├── Check If Two String Arrays are Equivalent.cpp ├── Check If a Word Occurs As a Prefix of Any Word in a Sentence.cpp ├── Check if All Characters Have Equal Number of Occurrences.cpp ├── Check if the Sentence Is Pangram.cpp ├── Climbing Stairs.cpp ├── Concatenation of Array.cpp ├── Contains Duplicate.cpp ├── Count Asterisks.cpp ├── Count Common Words With One Occurrence.cpp ├── Count Equal and Divisible Pairs in an Array.cpp ├── Count Good Triplets.cpp ├── Count Integers With Even Digit Sum.cpp ├── Count Items Matching a Rule.cpp ├── Count Largest Group.cpp ├── Count Negative Numbers in a Sorted Matrix.cpp ├── Count Number of Distinct Integers After Reverse Operations.cpp ├── Count Number of Pairs With Absolute Difference K.cpp ├── Count Number of Teams.cpp ├── Count Operations to Obtain Zero.cpp ├── Count Prefixes of a Given String.cpp ├── Count the Number of Consistent Strings.cpp ├── Counting Bits.cpp ├── Counting Words With a Given Prefix.cpp ├── Create Target Array in the Given Order.cpp ├── DI String Match.cpp ├── Day of the Week.cpp ├── Day of the Year.cpp ├── Decode XORed Array.cpp ├── Decode the Message.cpp ├── Decompress Run-Length Encoded List.cpp ├── Defanging an IP Address.cpp ├── Determine Color of a Chessboard Square.cpp ├── Determine if String Halves Are Alike.cpp ├── Divide Array Into Equal Pairs.cpp ├── Egg Drop With 2 Eggs and N Floors.cpp ├── Element Appearing More Than 25% In Sorted Array.cpp ├── Encode and Decode TinyURL.cpp ├── Fibonacci Number.cpp ├── Final Prices With a Special Discount in a Shop.cpp ├── Final Value of Variable After Performing Operations.cpp ├── Find All Duplicates in an Array.cpp ├── Find All K-Distant Indices in an Array.cpp ├── Find All Numbers Disappeared in an Array.cpp ├── Find Closest Number to Zero.cpp ├── Find First Palindromic String in the Array.cpp ├── Find First and Last Position of Element in Sorted Array.cpp ├── Find Greatest Common Divisor of Array.cpp ├── Find Lucky Integer in an Array.cpp ├── Find N Unique Integers Sum up to Zero.cpp ├── Find Nearest Point That Has the Same X or Y Coordinate.cpp ├── Find Numbers with Even Number of Digits.cpp ├── Find Target Indices After Sorting Array.cpp ├── Find the Difference of Two Arrays.cpp ├── Find the Difference.cpp ├── Find the Distance Value Between Two Arrays.cpp ├── Find the Duplicate Number.cpp ├── Find the Highest Altitude.cpp ├── Find the Kth Largest Integer in the Array.cpp ├── First Unique Character in a String.cpp ├── Fizz Buzz.cpp ├── Flipping an Image.cpp ├── Goal Parser Interpretation.cpp ├── Group Anagrams.cpp ├── Guess Number Higher or Lower.cpp ├── Height Checker.cpp ├── How Many Numbers Are Smaller Than the Current Number.cpp ├── Intersection of Two Arrays II.cpp ├── Intersection of Two Arrays.cpp ├── Is Subsequence.cpp ├── Jewels and Stones.cpp ├── K Closest Points to Origin.cpp ├── Keep Multiplying Found Values by Two.cpp ├── Keyboard Row.cpp ├── Kids With the Greatest Number of Candies.cpp ├── Kth Distinct String in an Array.cpp ├── Kth Largest Element in an Array.cpp ├── Kth Smallest Element in a Sorted Matrix.cpp ├── Largest Local Values in a Matrix.cpp ├── Largest Number At Least Twice of Others.cpp ├── Largest Positive Integer That Exists With Its Negative.cpp ├── Length of Last Word.cpp ├── Longest Continuous Increasing Subsequence.cpp ├── Longest Increasing Subsequence.cpp ├── Majority Element.cpp ├── Make Array Zero by Subtracting Equal Amounts.cpp ├── Make Two Arrays Equal by Reversing Subarrays.cpp ├── Matrix Diagonal Sum.cpp ├── Maximum 69 Number.cpp ├── Maximum Nesting Depth of the Parentheses.cpp ├── Maximum Number of Words Found in Sentences.cpp ├── Maximum Number of Words You Can Type.cpp ├── Maximum Product Difference Between Two Pairs.cpp ├── Maximum Product of Three Numbers.cpp ├── Maximum Product of Two Elements in an Array.cpp ├── Maximum Subarray.cpp ├── Maximum Sum of an Hourglass.cpp ├── Median of Two Sorted Arrays.cpp ├── Merge Similar Items.cpp ├── Merge Sorted Array.cpp ├── Minimize Maximum Pair Sum in Array.cpp ├── Minimum Number of Moves to Seat Everyone.cpp ├── Minimum Operations to Make the Array Increasing.cpp ├── Minimum Size Subarray Sum.cpp ├── Minimum Sum of Four Digit Number After Splitting Digits.cpp ├── Minimum Time Visiting All Points.cpp ├── Missing Number.cpp ├── Most Frequent Even Element.cpp ├── Most Frequent Number Following Key In an Array.cpp ├── Move Zeroes.cpp ├── N-Repeated Element in Size 2N Array.cpp ├── N-th Tribonacci Number.cpp ├── Next Permutation.cpp ├── Nim Game.cpp ├── Number Of Rectangles That Can Form The Largest Square.cpp ├── Number of Arithmetic Triplets.cpp ├── Number of Common Factors.cpp ├── Number of Days Between Two Dates.cpp ├── Number of Good Pairs.cpp ├── Number of Segments in a String.cpp ├── Number of Steps to Reduce a Number to Zero.cpp ├── Number of Strings That Appear as Substrings in Word.cpp ├── Number of Students Doing Homework at a Given Time.cpp ├── Palindrome Number.cpp ├── Pascal's Triangle II.cpp ├── Pascal's Triangle.cpp ├── Perfect Number.cpp ├── Perfect Number.py ├── Permutations II.cpp ├── Permutations.cpp ├── Plus One.cpp ├── Power of Four.cpp ├── Power of Three.cpp ├── Power of Two.cpp ├── README.md ├── Rank Transform of an Array.cpp ├── Ransom Note.cpp ├── Rearrange Array Elements by Sign.cpp ├── Rectangle Overlap.cpp ├── Replace All Digits with Characters.cpp ├── Replace Elements with Greatest Element on Right Side.cpp ├── Reverse Prefix of Word.cpp ├── Reverse String.cpp ├── Reverse Vowels of a String.cpp ├── Reverse Words in a String III.cpp ├── Richest Customer Wealth.cpp ├── Roman to Integer.cpp ├── Rotate Image.cpp ├── Rotate String.cpp ├── Running Sum of 1d Array.cpp ├── Search Insert Position.cpp ├── Search in Rotated Sorted Array.cpp ├── Search.cpp ├── Shuffle String.cpp ├── Shuffle the Array.cpp ├── Single Element in a Sorted Array.cpp ├── Single Number III.cpp ├── Single Number.cpp ├── Smallest Index With Equal Value.cpp ├── Smallest Value of the Rearranged Number.cpp ├── Sort Array By Parity II.cpp ├── Sort Array By Parity.cpp ├── Sort Array by Increasing Frequency.cpp ├── Sort Characters By Frequency.cpp ├── Sort Colors.cpp ├── Sort Colors.py ├── Sort Even and Odd Indices Independently.cpp ├── Sort Integers by The Number of 1 Bits.cpp ├── Sort Integers by The Power Value.cpp ├── Sort the People.cpp ├── Sorting the Sentence.cpp ├── Split a String in Balanced Strings.cpp ├── Sqrt(x).cpp ├── Squares of a Sorted Array.cpp ├── Substrings of Size Three with Distinct Characters.cpp ├── Sum of All Odd Length Subarrays.cpp ├── Sum of Digits in Base K.cpp ├── Sum of Number and Its Reverse.cpp ├── Sum of Two Integers.cpp ├── Sum of Two Integers.py ├── Sum of Unique Elements.cpp ├── The K Weakest Rows in a Matrix.cpp ├── Third Maximum Number.cpp ├── Three Consecutive Odds.cpp ├── To Lower Case.cpp ├── Top K Frequent Elements.cpp ├── Top K Frequent Words.cpp ├── Truncate Sentence.cpp ├── Two Out of Three.cpp ├── Two Sum.cpp ├── Ugly Number.cpp ├── Uncommon Words from Two Sentences.cpp ├── Unique Morse Code Words.cpp ├── Unique Number of Occurrences.cpp ├── Valid Anagram.cpp ├── Valid Parentheses.cpp ├── Valid Perfect Square.cpp ├── Valid Perfect Square.py ├── Water Bottles.cpp ├── Water Bottles.jav └── Word Pattern.cpp /Add Binary.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addBinary(self, a: str, b: str) -> str: 3 | sum=bin(int(a,2)+int(b,2)) 4 | return sum[2:] 5 | -------------------------------------------------------------------------------- /Add Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int n) { 4 | int s=0; 5 | while(n>0){ 6 | s+=n%10; 7 | n/=10; 8 | } 9 | if(s<10) return s; 10 | return addDigits(s); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Add Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addStrings(string s1, string s2) { 4 | int n1=s1.size(),n2=s2.size(),tmp=0,n=max(n1,n2); 5 | int s[n]; 6 | string str; 7 | reverse(s1.begin(),s1.end()); 8 | reverse(s2.begin(),s2.end()); 9 | if(n19)?1:0; 28 | } 29 | s[n-1]=(s1[n-1]-'0')+(s2[n-1]-'0')+tmp; 30 | reverse(s,s+n); 31 | for(int i=0;i& v) { 4 | sort(v.begin(),v.end()); 5 | int n=v.size(),s=0; 6 | for(int i=0;i& a) { 4 | int ma=*max_element(a.begin(),a.end()); 5 | int mi=*min_element(a.begin(),a.end()); 6 | double ans=0; 7 | int cnt=0; 8 | for(auto x:a) 9 | if(x!=mi and x!=ma) 10 | { 11 | ans+=x; 12 | ++cnt; 13 | } 14 | return ans/cnt; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Base 7.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToBase7(int num) { 4 | int n=abs(num); 5 | if(n==0) return "0"; 6 | stackst; 7 | while(n>0) 8 | { 9 | char c=(n%7)+'0'; 10 | st.push(c); 11 | n/=7; 12 | } 13 | string s=(num>=0)?"":"-"; 14 | while(!st.empty()) 15 | { 16 | s+=st.top(); 17 | st.pop(); 18 | 19 | } 20 | return s; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Build Array from Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector buildArray(vector& v) { 4 | int n=v.size(); 5 | vectora(n); 6 | for(int i=0;i cellsInRange(string s) { 4 | vectora; 5 | for(char i=s[0];i<=s[3];i++) 6 | for(char j=s[1];j<=s[4];j++) 7 | a.push_back({i,j}); 8 | return a; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Check If All 1's Are at Least Length K Places Away.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool kLengthApart(vector& v, int k) { 4 | int n=v.size(),id=0; 5 | for(int i=0;i& arr) { 4 | mapmp; 5 | for(auto x:arr) 6 | mp[x]++; 7 | int check=0; 8 | for(auto x:arr) 9 | if(mp[2*x] and x!=0 or(x==0 and mp[x]>1)) 10 | { 11 | check=1; 12 | break; 13 | } 14 | return check; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Check If String Is a Prefix of Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPrefixString(string s, vector& w) { 4 | string tmp; 5 | for(string x:w) 6 | { 7 | tmp+=x; 8 | if(tmp==s) return 1; 9 | } 10 | return 0; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Check If Two String Arrays are Equivalent.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool arrayStringsAreEqual(vector& w1, vector& w2) { 4 | string s1,s2; 5 | for(auto x:w1)s1+=x; 6 | for(auto x:w2)s2+=x; 7 | return s1==s2; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Check If a Word Occurs As a Prefix of Any Word in a Sentence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int isPrefixOfWord(string sentence, string searchWord) { 4 | int cnt=1; 5 | string tmp; 6 | stringstream ss(sentence); 7 | int n=searchWord.size(); 8 | while(ss>>tmp) 9 | { 10 | tmp=tmp.substr(0,n); 11 | if(tmp==searchWord) return cnt; 12 | cnt++; 13 | } 14 | return -1; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Check if All Characters Have Equal Number of Occurrences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool areOccurrencesEqual(string s) { 4 | mapmp; 5 | for(auto i:s) 6 | mp[i]++; 7 | for(auto i:s) 8 | if(mp[i]!=mp[s[0]]) 9 | return 0; 10 | return 1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Check if the Sentence Is Pangram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkIfPangram(string s) { 4 | setse; 5 | int n=s.length(); 6 | for(int i=0;i getConcatenation(vector& v) { 4 | int n=v.size(); 5 | vectora(2*n); 6 | for(int i=0;i<2*n;i++) a[i]=v[i%n]; 7 | return a; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Contains Duplicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector& v) { 4 | return v.size()>set(v.begin(),v.end()).size(); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Count Asterisks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countAsterisks(string s) { 4 | int n=s.length(),cnt=0,dem=0; 5 | for(int i=0;i& w1, vector& w2) { 4 | mapmp1,mp2; 5 | sets1,s2; 6 | for(auto x:w1) 7 | mp1[x]++; 8 | for(auto x:w2) 9 | mp2[x]++; 10 | for(auto x:w1) 11 | if(mp1[x]<2) 12 | s1.insert(x); 13 | for(auto x:w2) 14 | if(mp2[x]<2) 15 | s2.insert(x); 16 | int cnt=0; 17 | for(auto x:s1) 18 | if(s2.find(x)!=s2.end()) 19 | ++cnt; 20 | return cnt; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Count Equal and Divisible Pairs in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPairs(vector& a, int k) { 4 | int cnt=0; 5 | for(int i=0;i& arr, int a, int b, int c) { 4 | int n=arr.size(),cnt=0; 5 | for(int i=0;i>& items, string ruleKey, string ruleValue) { 4 | int x=(ruleKey=="type")?0:((ruleKey=="color")?1:2); 5 | int cnt=0; 6 | for(int i=0;imp; 15 | sets; 16 | int ans=0; 17 | for(int i=1;i<=n;i++) 18 | { 19 | s.insert(sum(i)); 20 | mp[sum(i)]++; 21 | } 22 | for(auto x:s) 23 | ans=max(ans,mp[x]); 24 | int cnt=0; 25 | for(auto x:s) 26 | if(mp[x]==ans) 27 | ++cnt; 28 | return cnt; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Count Negative Numbers in a Sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countNegatives(vector>& g) { 4 | int n=g.size(),cnt=0; 5 | for(int i=0;i& v) { 4 | vectora; 5 | sets; 6 | int n=v.size(); 7 | for(int i=0;i0) k=k*10+v[i]%10,v[i]/=10; 12 | s.insert(k); 13 | } 14 | return s.size(); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Count Number of Pairs With Absolute Difference K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countKDifference(vector& a, int k) { 4 | mapmp; 5 | setse; 6 | int cnt=0; 7 | for(auto x:a){ 8 | mp[x]++; 9 | se.insert(x); 10 | } 11 | for(auto x:se) cnt+=mp[x]*mp[x+k]; 12 | return cnt; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Count Number of Teams.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTeams(vector& a) { 4 | int cnt=0; 5 | int n=a.size(); 6 | for(int i=0;ia[i])++ll; 13 | else if(a[j]a[i])++rl; 18 | else if(a[j]0) 7 | { 8 | if(num1>=num2) 9 | num1-=num2; 10 | else num2-=num1; 11 | ++cnt; 12 | } 13 | return cnt; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Count Prefixes of a Given String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPrefixes(vector& w, string s) { 4 | int cnt=0; 5 | for(string x:w) 6 | { 7 | int n=x.size(),i=0,check=1; 8 | while(i& s) { 4 | int cnt=0; 5 | mapmp; 6 | int n=s.size(); 7 | for(int i=0;i>= 1; 9 | } 10 | return count; 11 | } 12 | vector countBits(int n) { 13 | vectorv(n+1); 14 | for(int i=0;i<=n;i++) 15 | v[i]=bit(i); 16 | return v; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Counting Words With a Given Prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int prefixCount(vector& w, string s) { 4 | int n=w.size(),m=s.size(),cnt=0; 5 | for(int i=0;i createTargetArray(vector& a, vector& id) { 4 | int n=a.size(); 5 | vectorv; 6 | for(int i=0;i diStringMatch(string s) { 4 | int n=s.size(),l=0,r=n; 5 | vectorv; 6 | for(int i=0;i<=n;i++) 7 | { 8 | if(s[i]=='I'||i==n) v.push_back(l++); 9 | else v.push_back(r--); 10 | } 11 | return v; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Day of the Week.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dayofmonth(int m,int y) 4 | { 5 | if(m==2) 6 | { 7 | if(y%400==0) return 29; 8 | else if(y%4==0&&y%100!=0) 9 | return 29; 10 | else return 28; 11 | } 12 | else if(m==4||m==6||m==9||m==11) return 30; 13 | else return 31; 14 | } 15 | int dayofyear(int y) 16 | { 17 | if(y%400==0) return 366; 18 | else if(y%4==0&&y%100!=0) 19 | return 366; 20 | else return 365; 21 | } 22 | string day(int n) 23 | { 24 | int x=(n)%7; 25 | switch(x) 26 | { 27 | case 4: 28 | return "Monday"; 29 | break; 30 | case 5: 31 | return "Tuesday"; 32 | break; 33 | case 6: 34 | return "Wednesday"; 35 | break; 36 | case 0: 37 | return "Thursday"; 38 | break; 39 | case 1: 40 | return "Friday"; 41 | break; 42 | case 2: 43 | return "Saturday"; 44 | break; 45 | case 3: 46 | return "Sunday"; 47 | break; 48 | 49 | } 50 | return ""; 51 | } 52 | string dayOfTheWeek(int d, int m, int y) { 53 | //1/1/1971 is Friday 54 | int n=0; 55 | if(y>1971) 56 | for(int i=1971;i1) 59 | for(int i=1;i decode(vector& encoded, int first) { 4 | int n=encoded.size(); 5 | vectorv(n+1); 6 | v[0]=first; 7 | for(int i=1;i<=n;i++) 8 | v[i]=encoded[i-1]^v[i-1]; 9 | return v; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Decode the Message.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string decodeMessage(string key, string mes) { 4 | mapmc; 5 | mapmp; 6 | int n=key.length(); 7 | char c='a'; 8 | for(int i=0;i'z') break; 11 | if(isalpha(key[i])&&mp[key[i]]<1){ 12 | mc[key[i]]=(char)c++; 13 | mp[key[i]]=1; 14 | } 15 | else continue; 16 | } 17 | string s; 18 | n=mes.length(); 19 | for(int i=0;i decompressRLElist(vector& a) { 4 | int n=a.size(); 5 | vectorv; 6 | for(int i=1;i& a) { 4 | sets; 5 | mapmp; 6 | for(auto x:a) 7 | { 8 | s.insert(x); 9 | mp[x]++; 10 | } 11 | for(auto x:s) if(mp[x]%2!=0) return 0; 12 | return 1; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Egg Drop With 2 Eggs and N Floors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int twoEggDrop(int n) { 4 | int cnt=1; 5 | while(n-cnt>0) 6 | n-=cnt++; 7 | return cnt>n?cnt:n; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Element Appearing More Than 25% In Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findSpecialInteger(vector& a) { 4 | mapmp; 5 | int n=a.size(); 6 | for(auto x:a){ 7 | mp[x]++; 8 | if(mp[x]>n/4) return x; 9 | } 10 | return -1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Encode and Decode TinyURL.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | // Encodes a URL to a shortened URL. 5 | string encode(string longUrl) { 6 | return longUrl; 7 | } 8 | 9 | // Decodes a shortened URL to its original URL. 10 | string decode(string shortUrl) { 11 | return shortUrl; 12 | } 13 | }; 14 | 15 | // Your Solution object will be instantiated and called as such: 16 | // Solution solution; 17 | // solution.decode(solution.encode(url)); 18 | -------------------------------------------------------------------------------- /Fibonacci Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fi[31]; 4 | void init() 5 | { 6 | fi[0]=0,fi[1]=1; 7 | for(int i=2;i<31;i++) 8 | fi[i]=fi[i-1]+fi[i-2]; 9 | } 10 | int fib(int n) { 11 | init(); 12 | return fi[n]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Final Prices With a Special Discount in a Shop.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector finalPrices(vector& p) { 4 | mapmp; 5 | vector v; 6 | int n=p.size(); 7 | for(int i=0;ii) v.push_back(p[i]-p[mp[i]]); 20 | else v.push_back(p[i]); 21 | } 22 | return v; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Final Value of Variable After Performing Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int finalValueAfterOperations(vector& s) { 4 | int x=0,n=s.size(); 5 | for(int i=0;i findDuplicates(vector& a) { 4 | mapmp; 5 | vectorv; 6 | int n=a.size(); 7 | sets(a.begin(),a.end()); 8 | for(int i=0;i findKDistantIndices(vector& a, int key, int k) { 4 | vectorv,id; 5 | int n=a.size(); 6 | for(int i=0;i findDisappearedNumbers(vector& a) { 4 | int n=a.size(); 5 | vectorv; 6 | mapmp; 7 | for(int i=0;i& a) { 4 | int n=a.size(),m=abs(a[0]); 5 | mapmp; 6 | for(int i=0;i& w) { 10 | string s=""; 11 | int n=w.size(); 12 | for(int i=0;i searchRange(vector& v, int t) { 4 | int n=v.size(); 5 | int check=0,l=-1,r=-1; 6 | for(int i=0;i=0;i--) 15 | if(v[i]==t) 16 | { 17 | r=i; 18 | break; 19 | } 20 | return {l,r}; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Find Greatest Common Divisor of Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int gcd(int a,int b) 4 | { 5 | if(a%b==0) return b; 6 | return gcd(b,a%b); 7 | } 8 | int findGCD(vector& a) { 9 | sort(a.begin(),a.end()); 10 | return gcd(a[0],a[a.size()-1]); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Find Lucky Integer in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLucky(vector& v) { 4 | mapmp; 5 | int n=v.size(); 6 | setse; 7 | for(int i=0;i sumZero(int n) { 4 | int k=n/2; 5 | vectorv; 6 | while(k>0){ 7 | v.push_back(-k); 8 | v.push_back(k--); 9 | } 10 | if(n%2!=0) v.push_back(0); 11 | return v; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Find Nearest Point That Has the Same X or Y Coordinate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int nearestValidPoint(int x, int y, vector>& p) { 5 | 6 | int ans=INT_MAX; 7 | for(auto i:p) 8 | if(i[0]==x or i[1]==y) 9 | ans=min(ans,((i[0]-x)*(i[0]-x)+(i[1]-y)*(i[1]-y))); 10 | for(int i=0;i& itg) { 4 | string tmp; 5 | int n=itg.size(); 6 | int cnt=0; 7 | for(int i=0;i targetIndices(vector&a, int target) { 4 | vectorid; 5 | int n=a.size(); 6 | sort(a.begin(),a.end()); 7 | for(int i=0;i> findDifference(vector& a1, vector& a2) { 4 | vector> v(2); 5 | sets1,s2; 6 | for(auto x:a1) s1.insert(x); 7 | for(auto x:a2) s2.insert(x); 8 | for(auto x:s1) if(s2.find(x)==s2.end()) (v[0]).push_back(x); 9 | for(auto x:s2) if(s1.find(x)==s1.end())( v[1]).push_back(x); 10 | return v; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Find the Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | setse; 5 | mapmp; 6 | for(auto x:s){ 7 | se.insert(x); 8 | mp[x]++; 9 | } 10 | for(auto x:t) { 11 | if(mp[x]>0) mp[x]--; 12 | else return x; 13 | } 14 | return 'c'; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Find the Distance Value Between Two Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findTheDistanceValue(vector& a1, vector& a2, int d) { 4 | int m=a1.size(),n=a2.size(),cnt=0; 5 | for(int i=0;i& a) { 4 | mapmp; 5 | for(auto x:a) 6 | { 7 | mp[x]++; 8 | if(mp[x]>1) return x; 9 | } 10 | return -1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Find the Highest Altitude.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestAltitude(vector& g) { 4 | int n=g.size(); 5 | int a[n+1]; 6 | a[0]=0; 7 | int m=0; 8 | for(int i=0;i& a, int k) { 10 | int n=a.size(); 11 | sort(a.begin(),a.end(),cmp); 12 | return a[n-k]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /First Unique Character in a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | mapmp; 5 | for(auto x:s)mp[x]++; 6 | int n=s.length(); 7 | for(int i=0;i fizzBuzz(int n) { 4 | vectora(n); 5 | for(int i=0;i> flipAndInvertImage(vector>& img) { 4 | for(int i=0;i> groupAnagrams(vector& s) { 4 | 5 | vector< pair > vp; 6 | set cnt; 7 | int j=0; 8 | for(auto x:s) 9 | { 10 | string k=x; 11 | sort(k.begin(),k.end()); 12 | vp.push_back(make_pair(x,k)); 13 | cnt.insert(k); 14 | } 15 | int n=vp.size(); 16 | vector>v(cnt.size()); 17 | for(auto x:cnt) 18 | { 19 | for(int i=0;i& h) { 4 | int cnt=0; 5 | vectora; 6 | a=h; 7 | int n=a.size(); 8 | sort(a.begin(),a.end()); 9 | for(int i=0;i smallerNumbersThanCurrent(vector& v) { 4 | mapmp; 5 | vectorvi; 6 | //[8,1,2,2,3] 7 | //1 1 2 2 1 8 | // 5 1 3 3 4 9 | int n=v.size(); 10 | for(int i=0;i intersect(vector& a1, vector& a2) { 4 | multisetms2; 5 | mapmp; 6 | vector v; 7 | for(auto x:a2)ms2.insert(x); 8 | for(auto x:a2)mp[x]++; 9 | for(auto x:a1) 10 | if(mp[x]>0&&ms2.find(x)!=ms2.end()) { 11 | v.push_back(x); 12 | mp[x]--; 13 | } 14 | return v; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Intersection of Two Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& a1, vector& a2) { 4 | sets,s2; 5 | vectorv; 6 | for(auto x:a1) s.insert(x); 7 | for(auto x:a2) s2.insert(x); 8 | for(auto x:s2) if(s.find(x)!=s.end()) v.push_back(x); 9 | return v; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Is Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int n=s.size(),m=t.size(),j=0; 5 | for(int i=0;i a,vector b) 4 | { 5 | return ((a[0]*a[0]+a[1]*a[1])<(b[0]*b[0]+b[1]*b[1])); 6 | } 7 | vector> kClosest(vector>& p, int k) { 8 | vector>v; 9 | sort(p.begin(),p.end(),cmp); 10 | for(int i=0;i& a, int o) { 4 | mapmp; 5 | int n=a.size(); 6 | for(auto x:a) mp[x]++; 7 | while(mp[o]>0)o*=2; 8 | return o; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Keyboard Row.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findWords(vector& s) { 4 | string s1="qwertyuiop",s2="asdfghjkl",s3="zxcvbnm"; 5 | mapmp; 6 | vectorv; 7 | for(auto x:s1) mp[x]=1; 8 | for(auto x:s2) mp[x]=2; 9 | for(auto x:s3) mp[x]=3; 10 | for(auto x:s) 11 | { 12 | string y=x; 13 | transform(x.begin(), x.end(), x.begin(), ::tolower); 14 | int check=1,n=x.size() ,k=mp[x[0]]; 15 | for(int i=1;i kidsWithCandies(vector& v, int c) { 4 | int n=v.size(); 5 | vector a(n); 6 | int m=0; 7 | for(int i=0;i=m; 9 | return a; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Kth Distinct String in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string kthDistinct(vector& arr, int k) { 4 | mapmp; 5 | int n,cnt=0; 6 | vectorv; 7 | for(auto x:arr) 8 | mp[x]++; 9 | for(auto x:arr) 10 | if(mp[x]<2) 11 | v.push_back(x); 12 | n=v.size(); 13 | if(k>n) return ""; 14 | return v[k-1]; 15 | 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Kth Largest Element in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& a, int k) { 4 | int n=a.size(); 5 | sort(a.begin(),a.end()); 6 | return a[n-k]; 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /Kth Smallest Element in a Sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthSmallest(vector>& matrix, int k) { 4 | vectorv; 5 | for(int i=0;i> largestLocal(vector>& g) { 4 | int n=g.size(); 5 | vector> a(n-2,vector(n-2)); 6 | for(int i=0;i& v) { 4 | vectora; 5 | a=v; 6 | mapmp; 7 | int n=a.size(); 8 | sort(a.begin(),a.end()); 9 | for(int i=0;ia[n-1])?(-1):(mp[a[n-1]]); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Largest Positive Integer That Exists With Its Negative.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxK(vector& v) { 4 | sort(v.begin(),v.end()); 5 | mapmp; 6 | int n=v.size(); 7 | int k=upper_bound(v.begin(),v.end(),0)-v.begin(); 8 | for(int i=0;i=k;i--) 11 | if(mp[-v[i]]>0) 12 | return v[i]; 13 | return -1; 14 | 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Length of Last Word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | int cnt=0; 5 | int i=s.length()-1; 6 | while(!isalpha(s[i])) i--; 7 | while(i>=0 and isalpha(s[i--])) ++cnt; 8 | return cnt; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Longest Continuous Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLengthOfLCIS(vector& a) { 4 | int ans=0; 5 | int n=a.size(),l[n]; 6 | l[0]=1; 7 | for(int i=1;ia[i-1])?(l[i]=l[i-1]+1):(l[i]=1); 9 | return *max_element(l,l+n); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& a) { 4 | int n=a.size(); 5 | int dp[n]; 6 | dp[0]=1; 7 | for(int i=1;ia[j]) 11 | dp[i]=max(dp[i],dp[j]+1); 12 | } 13 | return *max_element(dp,dp+n); 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Majority Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& v) { 4 | int n=v.size(); 5 | setse; 6 | mapmp; 7 | for(int i=0;in/2) 13 | return x; 14 | return -1; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Make Array Zero by Subtracting Equal Amounts.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumOperations(vector& nums) { 4 | int ans=set(nums.begin(),nums.end()).size()-(count(nums.begin(),nums.end(),0)>0); 5 | return ans; 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Make Two Arrays Equal by Reversing Subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canBeEqual(vector& t, vector& a) { 4 | mapmp; 5 | int n=t.size(); 6 | for(int i=0;i>& mat) { 4 | int n=mat.size(),s=0; 5 | for(int i=0;ist; 5 | int n=s.length(),cnt=0,m=0; 6 | for(int i=0;i& s) { 4 | int n=s.size(),m=0; 5 | for(int i=0;i>tmp)++cnt; 11 | m=max(m,cnt); 12 | } 13 | return m; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Maximum Number of Words You Can Type.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int canBeTypedWords(string text, string broken) { 4 | int cnt=0; 5 | string tmp; 6 | stringstream ss(text); 7 | vectorv; 8 | while(ss>>tmp) v.push_back(tmp); 9 | for(int i=0;i& a) { 4 | sort(a.begin(),a.end()); 5 | int n=a.size(); 6 | return a[n-2]*a[n-1]-a[0]*a[1]; 7 | 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Maximum Product of Three Numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumProduct(vector& v) { 4 | sort(v.begin(),v.end()); 5 | int n=v.size(); 6 | return max(v[0]*v[1]*v[n-1],v[n-3]*v[n-2]*v[n-1]); 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /Maximum Product of Two Elements in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& a) { 4 | sort(a.begin(),a.end()); 5 | int n=a.size(); 6 | return (a[n-1]-1)*(a[n-2]-1); 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /Maximum Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& a) { 4 | int n=a.size(); 5 | int dp[n]; 6 | dp[0]=a[0]; 7 | for(int i=1;i>& g) { 4 | int m=g.size(),n=g[0].size(),res=0; 5 | for(int i=0;i& a1, vector& a2) { 4 | vectorv; 5 | for(auto x:a1) v.push_back(x); 6 | for(auto x:a2) v.push_back(x); 7 | int n=v.size(); 8 | sort(v.begin(),v.end()); 9 | if(n%2!=0) return v[n/2]; 10 | return (v[n/2]+v[n/2 -1])/2.0; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Merge Similar Items.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> mergeSimilarItems(vector>& it1, vector>& it2) { 4 | vector> v; 5 | sets; 6 | mapmp; 7 | for(auto x:it1){ 8 | s.insert(x[0]); 9 | mp[x[0]]+=x[1]; 10 | } 11 | for(auto x:it2){ 12 | s.insert(x[0]); 13 | mp[x[0]]+=x[1]; 14 | } 15 | for(auto x:s) 16 | { 17 | vectorv1; 18 | v1.push_back(x); 19 | v1.push_back(mp[x]); 20 | v.push_back(v1); 21 | } 22 | return v; 23 | 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Merge Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | int i=m-1,j=n-1,k=m+n-1; 5 | while(i>=0&&j>=0){ 6 | if(nums1[i]==nums2[j]){ 7 | nums1[k--]=nums1[i--]; 8 | nums1[k--]=nums2[j--]; 9 | } 10 | else if(nums1[i]>nums2[j]){ 11 | nums1[k--]=nums1[i--]; 12 | } 13 | else{ 14 | nums1[k--]=nums2[j--]; 15 | } 16 | } 17 | while(i>=0) nums1[k--]=nums1[i--]; 18 | while(j>=0) nums1[k--]=nums2[j--]; 19 | 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Minimize Maximum Pair Sum in Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPairSum(vector& v) { 4 | sort(v.begin(),v.end()); 5 | int n=v.size(); 6 | int res=v[n-1]+v[0]; 7 | for(int i=0;i& seats, vector& students) { 4 | int cnt=0; 5 | sort(seats.begin(),seats.end()); 6 | sort(students.begin(),students.end()); 7 | for(int i=0;i& a) { 4 | int n=a.size(),cnt=0; 5 | for(int i=1;ia[i-1]) continue; 8 | else { 9 | cnt+=a[i-1]-a[i]+1; 10 | a[i]=a[i-1]+1; 11 | } 12 | } 13 | return cnt; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Minimum Size Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int target, vector& nums) { 4 | int n=nums.size(),i=0,j=0,s=0,cnt=n+1; 5 | while(j=target) 9 | { 10 | cnt=min(cnt,j-i); 11 | s-=nums[i++]; 12 | } 13 | } 14 | return cnt%(n+1); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Minimum Sum of Four Digit Number After Splitting Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumSum(int num) { 4 | vectorv; 5 | while(num>0) 6 | { 7 | v.push_back(num%10); 8 | num/=10; 9 | } 10 | sort(v.begin(),v.end()); 11 | int k=10*(v[0]+v[1])+v[2]+v[3]; 12 | return k; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Minimum Time Visiting All Points.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minTimeToVisitAllPoints(vector>& s) { 4 | int min=0,n=s.size(); 5 | for(int i=1;i& v) { 4 | int n=v.size(),m=v[0]; 5 | mapmp; 6 | mp.clear(); 7 | for(int i=0;i& a) { 4 | mapmp; 5 | setse; 6 | int n=a.size(),k=0; 7 | for(auto x:a) 8 | { 9 | if(x%2!=0) continue; 10 | mp[x]++; 11 | se.insert(x); 12 | k=max(k,mp[x]); 13 | } 14 | for(auto x:se) 15 | if(mp[x]==k) 16 | return x; 17 | return -1; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Most Frequent Number Following Key In an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mostFrequent(vector& a, int key) { 4 | int n=a.size(); 5 | sets; 6 | mapmp; 7 | for(int i=0;ires) 15 | ans=x,res=mp[x]; 16 | return ans; 17 | 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Move Zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector& a) { 4 | int n=a.size(); 5 | vectormts; 6 | for(auto x:a) if(x!=0) mts.push_back(x); 7 | int m=mts.size(),i=0; 8 | for(auto x:mts) a[i++]=x; 9 | for(int i=m;i& a) { 4 | mapmp; 5 | int n=a.size(); 6 | for(auto x:a)mp[x]++; 7 | for(auto x:a) if(mp[x]==n/2) return x; 8 | return -1; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /N-th Tribonacci Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tri[38]; 4 | void init() 5 | { 6 | tri[0]=0; 7 | tri[1]=1; 8 | tri[2]=1; 9 | for(int i=3;i<38;i++) 10 | tri[i]=tri[i-1]+tri[i-2]+tri[i-3]; 11 | } 12 | int tribonacci(int n) { 13 | init(); 14 | return tri[n]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Next Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& v) { 4 | next_permutation(v.begin(),v.end()); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Nim Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canWinNim(int n) { 4 | return n%4; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Number Of Rectangles That Can Form The Largest Square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countGoodRectangles(vector>& a) { 4 | int m=0; 5 | int n=a.size(); 6 | mapmp; 7 | for(int i=0;i& a, int k) { 4 | mapmp; 5 | int n=a.size(),cnt=0; 6 | for(int i=0;ia) swap(a,b); 6 | for(int i=2;i<=sqrt(b);i++) 7 | { 8 | if(b%i==0&&a%i==0&&i*i!=b)++cnt; 9 | if(b%i==0&&a%(b/i)==0&&i*i!=b)++cnt; 10 | if(b%i==0&&a%i==0&&i*i==b)++cnt; 11 | } 12 | if(a%b==0&&a>1)++cnt; 13 | return cnt; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Number of Days Between Two Dates.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int doy(int y) 4 | { 5 | if(y%400==0) return 366; 6 | else if(y%4==0 and y%100!=0) return 366; 7 | return 365; 8 | } 9 | int dom(int m,int y) 10 | { 11 | if(m==2) { 12 | if(y%400==0) return 29; 13 | else if(y%4==0 and y%100!=0) return 29; 14 | return 28; 15 | } 16 | else if(m==4||m==6||m==9||m==11) return 30; 17 | 18 | return 31; 19 | } 20 | int thy(int d,int m,int y) 21 | { 22 | if(m<2) return d; 23 | int s=0; 24 | for(int i=1;iy2 or (y1==y2 and m1>m2) ) { 64 | swap(y1,y2); 65 | swap(m1,m2); 66 | swap(d1,d2); 67 | } 68 | return between(d1,m1,y1,d2,m2,y2); 69 | } 70 | }; 71 | -------------------------------------------------------------------------------- /Number of Good Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numIdenticalPairs(vector& v) { 4 | mapmp; 5 | sets; 6 | int n=v.size(); 7 | for(int i=0;i>tmp)++cnt; 8 | return cnt; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Number of Steps to Reduce a Number to Zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfSteps(int num) { 4 | int cnt=0; 5 | while(num) 6 | { 7 | if(num%2==0)num/=2; 8 | else num-=1; 9 | ++cnt; 10 | } 11 | return cnt; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Number of Strings That Appear as Substrings in Word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numOfStrings(vector& v, string word) { 4 | int cnt=0,n=v.size(); 5 | for(int i=0;i& st, vector& en, int q) { 4 | int n=st.size(),cnt=0; 5 | for(int i=0;i=q)++cnt; 7 | return cnt; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Palindrome Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(long long x) { 4 | long long k=x,n=0; 5 | while(x>0){ 6 | n=n*10+x%10; 7 | x/=10; 8 | } 9 | return n==k; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Pascal's Triangle II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getRow(int r) { 4 | vectorv(r+1); 5 | v[0]=1; 6 | for(int i=0;i<=r;i++) 7 | for(int j=i;j>0;j--) 8 | v[j]+=v[j-1]; 9 | return v; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Pascal's Triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int n) { 4 | vector> v; 5 | for(int i=1;i<=n;i++){ 6 | vectorv1(i,1); 7 | for(int j=1;j bool: 3 | return (n==6 or n== 28 or n== 496 or n== 8128 or n==33550336 or n==8589869056 or n==137438691328) 4 | -------------------------------------------------------------------------------- /Permutations II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> permuteUnique(vector& a) { 4 | vector> v; 5 | 6 | sort(a.begin(),a.end()); 7 | do{ 8 | v.push_back(a); 9 | }while(next_permutation(a.begin(),a.end())); 10 | return v; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> permute(vector& a) { 4 | sort(a.begin(),a.end()); 5 | vector> v; 6 | do{ 7 | v.push_back(a); 8 | }while(next_permutation(a.begin(),a.end())); 9 | return v; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Plus One.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector& v) { 4 | int n=v.size(),i=n-1,tmp=0; 5 | if(v[0]==9&&n==1) { v[0]=1; v.insert(v.begin()+1,0); return v; } 6 | if(v[i]+1>=10) v[i--]=0,tmp=1; 7 | while(v[i]+tmp>=10&&i>0) 8 | { 9 | v[i--]=0; 10 | } 11 | v[i]+=1; 12 | if(v[i]>9) { 13 | v[i]=0; 14 | v.insert(v.begin(),1); 15 | } 16 | return v; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Power of Four.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfFour(int n) { 4 | if(n<1) return 0; 5 | while(n>=4) 6 | { 7 | if(n%4!=0) return 0; 8 | n/=4; 9 | } 10 | return n==1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Power of Three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | if(n<1) return 0; 5 | while(n>=3) 6 | { 7 | if(n%3!=0) return 0; 8 | n/=3; 9 | } 10 | return n==1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Power of Two.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | while(n>2) 5 | { 6 | if(n%2!=0) return 0; 7 | n/=2; 8 | } 9 | return n>=1; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #LeetCode Solution 2 | -------------------------------------------------------------------------------- /Rank Transform of an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector arrayRankTransform(vector& a) { 4 | sets(a.begin(),a.end()); 5 | maprank; 6 | vector v; 7 | int cnt=1; 8 | for(auto x:s) 9 | rank[x]=cnt++; 10 | for(auto x:a) 11 | v.push_back(rank[x]); 12 | return v; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Ransom Note.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string a, string b) { 4 | mapmp; 5 | for(auto i:b)mp[i]++; 6 | for(auto i:a){ 7 | if(!mp[i]) return 0; 8 | mp[i]--; 9 | } 10 | return 1; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Rearrange Array Elements by Sign.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector rearrangeArray(vector& v) { 4 | int n=v.size(); 5 | vector a(n); 6 | int i=0,j=1; 7 | for(auto x:v) 8 | { 9 | if(x>0) 10 | { 11 | a[i]=x; 12 | i+=2; 13 | } 14 | else{ 15 | a[j]=x; 16 | j+=2; 17 | } 18 | } 19 | return a; 20 | 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Rectangle Overlap.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isRectangleOverlap(vector& a, vector& b) { 4 | return ((a[0]b[1])); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Replace All Digits with Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string replaceDigits(string s) { 4 | for(int i=0;i replaceElements(vector& a) { 4 | int n=a.size(); 5 | vector v(n); 6 | int m=-1; 7 | v[n-1]=m; 8 | for(int i=n-1;i>0;i--) 9 | v[i-1]=max(v[i],m=a[i]); 10 | return v; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Reverse Prefix of Word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reversePrefix(string word, char ch) { 4 | int id=0,n=word.size(); 5 | for(int i=0;i& s) { 4 | reverse(s.begin(),s.end()); 5 | 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Reverse Vowels of a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool vowels(char c) 4 | { 5 | return (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='O'||c=='I'||c=='U'); 6 | } 7 | string reverseVowels(string s) { 8 | int l=0,r=s.length()-1,n=s.length(); 9 | while(l=0 and !vowels(s[r]))r--; 13 | if(l>tmp) 7 | { 8 | reverse(tmp.begin(),tmp.end()); 9 | s1+=tmp; 10 | s1+=" "; 11 | } 12 | while(s1[s1.size()-1]==' ') s1.erase(s1.size()-1,1); 13 | return s1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Richest Customer Wealth.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumWealth(vector>& v) { 4 | int m=0,n=v.size(); 5 | for(int i=0;imp { 5 | {'I',1}, 6 | {'V',5}, 7 | {'X',10}, 8 | {'L',50}, 9 | {'C',100}, 10 | {'D',500}, 11 | {'M',1000} 12 | }; 13 | int hex=mp[s[s.length()-1]]; 14 | //XIX 15 | //10-1=9 16 | for(int i=s.length()-1;i>0;i--){ 17 | if(mp[s[i]]<=mp[s[i-1]]) hex+=mp[s[i-1]]; 18 | else hex-=mp[s[i-1]]; 19 | } 20 | return hex; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& a) { 4 | int n=a.size(); 5 | for(int i=0;i runningSum(vector& v) { 4 | int n=v.size(); 5 | for(int i=1;i& nums, int target) { 4 | return lower_bound(nums.begin(),nums.end(),target)-nums.begin(); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Search in Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& v, int target) { 4 | int n=v.size(); 5 | for(int i=0;i& nums, int target) { 4 | int n=nums.size(); 5 | for(int i=0;i& v) { 4 | string a; 5 | int n=v.size(); 6 | mapmp; 7 | for(int i=0;i shuffle(vector& v, int n) { 4 | vectora(2*n); 5 | int j=0; 6 | for(int i=0;i& v) { 4 | int n=v.size(); 5 | for(int i=1;i1 and v[n-1]!=v[n-2])?v[n-1]:v[0]; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Single Number III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector singleNumber(vector& a) { 4 | vectorv; 5 | mapmp; 6 | sets; 7 | for(auto x:a) 8 | { 9 | mp[x]++; 10 | s.insert(x); 11 | } 12 | for(auto x:s) if(mp[x]<2) v.push_back(x); 13 | return v; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Single Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& v) { 4 | mapmp; 5 | for(auto x:v) mp[x]++; 6 | for(int i=0;i& a) { 4 | for(int i=0;iv; 6 | while(n>0){ 7 | v.push_back(n%10); 8 | n/=10; 9 | } 10 | sort(v.begin(),v.end()); 11 | if(num<=0) 12 | { 13 | long long ans=0; 14 | for(int i=v.size()-1;i>=0;i--) 15 | ans=ans*10+v[i]; 16 | return -ans; 17 | } 18 | int id=upper_bound(v.begin(),v.end(),0)-v.begin(); 19 | long long k=v[id]; 20 | for(int i=0;i sortArrayByParityII(vector& v) { 4 | 5 | int n=v.size(); 6 | vectore; 7 | vectoro; 8 | for(auto x:v){ 9 | if(x%2==0) 10 | e.push_back(x); 11 | else o.push_back(x); 12 | } 13 | int j=0,k=0; 14 | for(int i=0;i sortArrayByParity(vector& nums) { 4 | vectore,o,a; 5 | for(int i=0;i frequencySort(vector& a) { 4 | vectorv,w; 5 | setse; 6 | mapmp; 7 | for(auto x:a) 8 | { 9 | se.insert(x); 10 | mp[x]++; 11 | } 12 | for(auto x:se) 13 | w.push_back(x); 14 | int n=w.size(); 15 | for(int i=0;iw[i])) 18 | swap(w[i],w[j]); 19 | for(auto x:w) 20 | for(int i=0;imc; 7 | vectorv; 8 | setse; 9 | string str; 10 | for(auto i:s){ 11 | mc[i]++; 12 | se.insert(i); 13 | } 14 | for(auto x:se) v.push_back(x); 15 | int n=v.size(); 16 | for(int i=0;imc[v[i]]) 19 | swap(v[i],v[j]); 20 | for(auto x:v) 21 | for(int i=0;i& v) { 4 | sort(v.begin(),v.end()); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Sort Colors.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def sortColors(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: None Do not return anything, modify nums in-place instead. 6 | """ 7 | nums.sort() 8 | 9 | -------------------------------------------------------------------------------- /Sort Even and Odd Indices Independently.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortEvenOdd(vector& a) { 4 | int n=a.size(); 5 | 6 | for(int i=0;ia[i]) 13 | swap(a[i],a[j]); 14 | return a; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Sort Integers by The Number of 1 Bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | static int bit(int n) 4 | { 5 | int count = 0; 6 | while (n) { 7 | count += n & 1; 8 | n >>= 1; 9 | } 10 | return count; 11 | } 12 | static bool cmp(const int &a,const int &b) 13 | { 14 | if(bit(a)!=bit(b)) return bit(a) sortByBits(vector& v) { 18 | std::sort(v.begin(),v.end(),cmp); 19 | return v; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Sort Integers by The Power Value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int power(int n) 4 | { 5 | int cnt=0; 6 | while(n!=1) 7 | { 8 | if(n%2==0) n/=2,++cnt; 9 | else n=n*3+1,++cnt; 10 | } 11 | return cnt; 12 | } 13 | static bool cmp(pair a,pair b) 14 | { 15 | if(a.second!=b.second) return a.second>vp; 20 | for(int i=a;i<=b;i++) 21 | vp.push_back(make_pair(i,power(i))); 22 | sort(vp.begin(),vp.end(),cmp); 23 | return vp[k-1].first; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Sort the People.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool cmp(paira,pairb) 4 | { 5 | return a.first>b.first; 6 | }; 7 | vector sortPeople(vector& name, vector& h) { 8 | vector>mv; 9 | vectorvs; 10 | int n=h.size(); 11 | for(int i=0;imv[i].first) 16 | swap(mv[i],mv[j]); 17 | for(int i=0;ivs; 5 | vectorvi; 6 | mapmp; 7 | string tmp,s1=""; 8 | stringstream ss(s); 9 | int m=0; 10 | while(ss>>tmp) 11 | { 12 | int n=tmp.size()-1; 13 | int k=tmp[n]-'0'; 14 | tmp.erase(n,1); 15 | mp[k]=tmp; 16 | m=max(m,k); 17 | } 18 | for(int i=0;imp; 6 | setse; 7 | for(int i=0;i sortedSquares(vector& v) { 4 | for(int i=0;ise; 6 | for(int i=0;i2)++cnt; 11 | } 12 | return cnt; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Sum of All Odd Length Subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOddLengthSubarrays(vector& arr) { 4 | int sum=0; 5 | int n=arr.size(); 6 | for(int i=0;ist; 5 | while(n>0) 6 | { 7 | st.push(n%k); 8 | n/=k; 9 | } 10 | int s=0; 11 | while(!st.empty()) 12 | { 13 | s+=st.top(); 14 | st.pop(); 15 | } 16 | return s; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Sum of Number and Its Reverse.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rev(int n) 4 | { 5 | int k=0; 6 | while(n>0) 7 | { 8 | k=k*10+n%10; 9 | n/=10; 10 | } 11 | return k; 12 | } 13 | bool sumOfNumberAndReverse(int num) { 14 | for(int i=num/2;i<=num;i++) 15 | if(i+rev(i)==num) return 1; 16 | return 0; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Sum of Two Integers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getSum(int a, int b) { 4 | return a+b; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Sum of Two Integers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSum(self, a: int, b: int) -> int: 3 | return a+b 4 | -------------------------------------------------------------------------------- /Sum of Unique Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOfUnique(vector& a) { 4 | int sum=0; 5 | mapmp; 6 | for(auto x:a) mp[x]++; 7 | for(auto x:a) if(mp[x]<2) sum+=x; 8 | return sum; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /The K Weakest Rows in a Matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector kWeakestRows(vector>& mat, int k) { 4 | int m=mat.size(),n=mat[0].size(); 5 | vector>v; 6 | vectorid; 7 | for(int i=0;i& v) { 4 | vectorvt; 5 | sets(v.begin(),v.end()); 6 | for(auto x:s)vt.push_back(x); 7 | int n=vt.size(); 8 | return (s.size()<3)?(vt[n-1]):(vt[n-3]); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Three Consecutive Odds.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool odd(int n) 4 | { 5 | return n%2!=0; 6 | } 7 | bool threeConsecutiveOdds(vector& a) { 8 | int n=a.size(); 9 | if(n<3) return 0; 10 | for(int i=1;i a,pair b) 4 | { 5 | return a.second topKFrequent(vector& a, int k) { 8 | setse; 9 | vectorv; 10 | vector>vp; 11 | mapmp; 12 | for(auto x:a) 13 | { 14 | se.insert(x); 15 | mp[x]++; 16 | } 17 | for(auto x:se) 18 | vp.push_back(make_pair(x,mp[x])); 19 | sort(vp.begin(),vp.end(),cmp); 20 | int n=vp.size(); 21 | for(int i=n-k;i topKFrequent(vector& ws, int k) { 4 | 5 | 6 | mapmp; 7 | setse; 8 | vectors,v; 9 | for(auto tmp:ws) { 10 | mp[tmp]++; 11 | se.insert(tmp); 12 | } 13 | for(auto x:se) s.push_back(x); 14 | int n=s.size(); 15 | for(int i=0;imp[s[i]] or (mp[s[i]] == mp[s[j]] and s[i]>s[j])) 18 | swap(s[i],s[j]); 19 | 20 | for(int i=0;i>tmp) 8 | { 9 | cnt++; 10 | s1+=tmp; 11 | if(cnt==k) break; 12 | s1+=" "; 13 | } 14 | return s1; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Two Out of Three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoOutOfThree(vector& n1, vector& n2, vector& n3) { 4 | vectora; 5 | int cnt[3][101]={}; 6 | for(auto x:n1) 7 | cnt[0][x]=1; 8 | for(auto x:n2) 9 | cnt[1][x]=1; 10 | for(auto x:n3) 11 | cnt[2][x]=1; 12 | for(int i=0;i<101;i++) 13 | if(cnt[0][i]+cnt[1][i]+cnt[2][i]>=2) 14 | a.push_back(i); 15 | return a; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Two Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | vectorv; 5 | for(int i=0;i5) return 0; 9 | while(n%i==0)n/=i; 10 | } 11 | } 12 | return n<7; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Uncommon Words from Two Sentences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector uncommonFromSentences(string s1, string s2) { 4 | stringstream ss1(s1); 5 | stringstream ss2(s2); 6 | mapmp; 7 | sets; 8 | vectorv; 9 | string tmp; 10 | while(ss1>>tmp) 11 | { 12 | mp[tmp]++; 13 | s.insert(tmp); 14 | } 15 | while(ss2>>tmp) 16 | { 17 | mp[tmp]++; 18 | s.insert(tmp); 19 | } 20 | for(auto x:s) 21 | if(mp[x]<2) 22 | v.push_back(x); 23 | return v; 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Unique Morse Code Words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniqueMorseRepresentations(vector& w) { 4 | map morse{ 5 | {'a',".-"}, 6 | {'b',"-..."}, 7 | {'c',"-.-."}, 8 | {'d',"-.."}, 9 | {'e',"."}, 10 | {'f',"..-."}, 11 | {'g',"--."}, 12 | {'h',"...."}, 13 | {'i',".."}, 14 | {'j',".---"}, 15 | {'k',"-.-"}, 16 | {'l',".-.."}, 17 | {'m',"--"}, 18 | {'n',"-."}, 19 | {'o',"---"}, 20 | {'p',".--."}, 21 | {'q',"--.-"}, 22 | {'r',".-."}, 23 | {'s',"..."}, 24 | {'t',"-"}, 25 | {'u',"..-"}, 26 | {'v',"...-"}, 27 | {'w',".--"}, 28 | {'x',"-..-"}, 29 | {'y',"-.--"}, 30 | {'z',"--.."} 31 | }; 32 | setse; 33 | int n=w.size(); 34 | for(int i=0;i& arr) { 4 | mapmp; 5 | sets1,s2; 6 | for(auto x:arr) 7 | { 8 | mp[x]++; 9 | s1.insert(x); 10 | } 11 | for(auto x:s1) 12 | s2.insert(mp[x]); 13 | return s1.size()==s2.size(); 14 | 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Valid Anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | if(s.length()!=t.length()) return 0; 5 | int n=s.length(); 6 | mapmp; 7 | for(int i=0;ist; 5 | int n=s.length(); 6 | 7 | for(auto x:s) 8 | { 9 | if(x=='(' or x=='{' or x=='[') st.push(x); 10 | else { 11 | if (st.size()==0 or (x != '}' and st.top() == '{') or (x != ']' and st.top() == '[') or (x != ')' and st.top() == '(') ) return false; 12 | //if(st.empty() or (st.top()=='(' and i!=')') or (st.top()=='{' and i!='}') or (st.top()=='[' and i!=']')) return false; 13 | 14 | st.pop(); 15 | } 16 | } 17 | 18 | return st.size()==0; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Valid Perfect Square.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int num) { 4 | int k=sqrt(num); 5 | return k*k==num; 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Valid Perfect Square.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPerfectSquare(self, num: int) -> bool: 3 | k=(int)(math.sqrt(num)) 4 | return k*k==num 5 | -------------------------------------------------------------------------------- /Water Bottles.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numWaterBottles(int a, int b) { 4 | return (av; 5 | string tmp; 6 | mapmc; 7 | mapms; 8 | mc.clear(); 9 | ms.clear(); 10 | stringstream ss(s); 11 | while(ss>>tmp) 12 | { 13 | v.push_back(tmp); 14 | } 15 | if(v.size()!=str.size()) return 0; 16 | int n=v.size(); 17 | for(int i=0;i