Skip to content

Commit 13d3453

Browse files
authored
Add files via upload
0 parents  commit 13d3453

File tree

100 files changed

+6171
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+6171
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 1047. Remove All Adjacent Duplicates In String
2+
// Solved
3+
// Easy
4+
// Topics
5+
// Companies
6+
// Hint
7+
// You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
8+
9+
// We repeatedly make duplicate removals on s until we no longer can.
10+
11+
// Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
12+
13+
14+
15+
// Example 1:
16+
17+
// Input: s = "abbaca"
18+
// Output: "ca"
19+
// Explanation:
20+
// For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
21+
// Example 2:
22+
23+
// Input: s = "azxxzy"
24+
// Output: "ay"
25+
26+
27+
// Constraints:
28+
29+
// 1 <= s.length <= 105
30+
// s consists of lowercase English letters.
31+
32+
33+
class Solution {
34+
public:
35+
string removeDuplicates(std::string s) {
36+
string ans;
37+
for(char c:s){
38+
if(!ans.empty() && ans.back()==c){
39+
ans.pop_back();
40+
}else{
41+
ans.push_back(c);
42+
}
43+
}
44+
return ans;
45+
}
46+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// 1071. Greatest Common Divisor of Strings
2+
// Easy
3+
// Topics
4+
// Companies
5+
// Hint
6+
// For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times).
7+
8+
// Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.
9+
10+
11+
12+
// Example 1:
13+
14+
// Input: str1 = "ABCABC", str2 = "ABC"
15+
// Output: "ABC"
16+
// Example 2:
17+
18+
// Input: str1 = "ABABAB", str2 = "ABAB"
19+
// Output: "AB"
20+
// Example 3:
21+
22+
// Input: str1 = "LEET", str2 = "CODE"
23+
// Output: ""
24+
25+
26+
// Constraints:
27+
28+
// 1 <= str1.length, str2.length <= 1000
29+
// str1 and str2 consist of English uppercase letters.
30+
31+
32+
class Solution {
33+
public:
34+
string gcdOfStrings(string str1, string str2) {
35+
int n1=str1.size();
36+
int n2=str2.size();
37+
if(str1+str2 != str2+str1){
38+
return "";
39+
}
40+
return str1.substr(0,gcd(n1,n2));
41+
}
42+
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// 1171. Remove Zero Sum Consecutive Nodes from Linked List
2+
// Medium
3+
// Topics
4+
// Companies
5+
// Hint
6+
// Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.
7+
8+
// After doing so, return the head of the final linked list. You may return any such answer.
9+
10+
11+
12+
// (Note that in the examples below, all sequences are serializations of ListNode objects.)
13+
14+
// Example 1:
15+
16+
// Input: head = [1,2,-3,3,1]
17+
// Output: [3,1]
18+
// Note: The answer [1,2,1] would also be accepted.
19+
// Example 2:
20+
21+
// Input: head = [1,2,3,-3,4]
22+
// Output: [1,2,4]
23+
// Example 3:
24+
25+
// Input: head = [1,2,3,-3,-2]
26+
// Output: [1]
27+
28+
29+
// Constraints:
30+
31+
// The given linked list will contain between 1 and 1000 nodes.
32+
// Each node in the linked list has -1000 <= node.val <= 1000.
33+
34+
35+
/**
36+
* Definition for singly-linked list.
37+
* struct ListNode {
38+
* int val;
39+
* ListNode *next;
40+
* ListNode() : val(0), next(nullptr) {}
41+
* ListNode(int x) : val(x), next(nullptr) {}
42+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
43+
* };
44+
// */
45+
// class Solution {
46+
// public:
47+
// ListNode* removeZeroSumSublists(ListNode* head) {
48+
// ListNode* temp=new ListNode(0);
49+
// temp->next=head;
50+
// int prefix=0;
51+
// unordered_map<int, ListNode*>mp;
52+
// mp[0]=temp;
53+
// while(head!=NULL){
54+
// prefix+=head->val;
55+
// mp[prefix]=head;
56+
// head=head->next;
57+
// }
58+
// head=temp;
59+
// prefix=0;
60+
// while(head!=NULL){
61+
// prefix+=head->val;
62+
// head->next=mp[prefix]->next;
63+
// head=head->next;
64+
// }
65+
// return temp->next;
66+
// }
67+
// };
68+
69+
class Solution {
70+
public:
71+
ListNode* removeZeroSumSublists(ListNode* head) {
72+
unordered_map<int,ListNode*> mp;
73+
ListNode* dummy=new ListNode(0);
74+
dummy->next=head;
75+
int prefix=0;
76+
mp[0]=dummy;
77+
while(head!=NULL){
78+
prefix+=head->val;
79+
if (mp.find(prefix)!=mp.end()){
80+
ListNode* prev=mp[prefix]->next;
81+
int sum=prefix+prev->val;
82+
while(sum!=prefix) {
83+
mp.erase(sum);
84+
prev=prev->next;
85+
sum+=prev->val;
86+
}
87+
mp[prefix]->next=head->next;
88+
} else {
89+
mp[prefix]=head;
90+
}
91+
head=head->next;
92+
}
93+
94+
return dummy->next;
95+
}
96+
};

12. Integer to Roman.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// 12. Integer to Roman
2+
// Medium
3+
// Topics
4+
// Companies
5+
// Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
6+
7+
// Symbol Value
8+
// I 1
9+
// V 5
10+
// X 10
11+
// L 50
12+
// C 100
13+
// D 500
14+
// M 1000
15+
// For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
16+
17+
// Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
18+
19+
// I can be placed before V (5) and X (10) to make 4 and 9.
20+
// X can be placed before L (50) and C (100) to make 40 and 90.
21+
// C can be placed before D (500) and M (1000) to make 400 and 900.
22+
// Given an integer, convert it to a roman numeral.
23+
24+
25+
26+
// Example 1:
27+
28+
// Input: num = 3
29+
// Output: "III"
30+
// Explanation: 3 is represented as 3 ones.
31+
// Example 2:
32+
33+
// Input: num = 58
34+
// Output: "LVIII"
35+
// Explanation: L = 50, V = 5, III = 3.
36+
// Example 3:
37+
38+
// Input: num = 1994
39+
// Output: "MCMXCIV"
40+
// Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
41+
42+
43+
// Constraints:
44+
45+
// 1 <= num <= 3999
46+
47+
// class Solution {
48+
// public:
49+
// string intToRoman(int num) {
50+
// string Roman="";
51+
// vector<pair<int,string>>
52+
// storeIntRoman={
53+
// {1000,"M"},
54+
// {900,"CM"},
55+
// {500,"D"},
56+
// {400,"CD"},
57+
// {100,"C"},
58+
// {90,"XC"},
59+
// {50,"L"},
60+
// {40,"XL"},
61+
// {10,"X"},
62+
// {9,"IX"},
63+
// {5,"V"},
64+
// {4,"IV"},
65+
// {1,"I"}
66+
// };
67+
68+
// for(int i=0;i<storeIntRoman.size();i++){
69+
// while(num>=storeIntRoman[i].first){
70+
// Roman+=storeIntRoman[i].second;
71+
// num-=storeIntRoman[i].first;
72+
// }
73+
// }
74+
// return Roman;
75+
// }
76+
// };
77+
78+
//Another Method -> thou[num/1000] + hund[(num%1000)/100] + tens[(num%100)/10] + ones[num%10];
79+
80+
class Solution {
81+
public:
82+
string intToRoman(int num) {
83+
string roman="";
84+
while(num>=1000){
85+
roman+="M";
86+
num-=1000;
87+
}
88+
if(num>=900){
89+
roman+="CM";
90+
num-=900;
91+
}
92+
while(num>=500){
93+
roman+="D";
94+
num-=500;
95+
}
96+
if(num>=400){
97+
roman+="CD";
98+
num-=400;
99+
}
100+
while(num>=100){
101+
roman+="C";
102+
num-=100;
103+
}
104+
if(num>=90){
105+
roman+="XC";
106+
num-=90;
107+
}
108+
if(num>=50){
109+
roman+="L";
110+
num-=50;
111+
}
112+
if(num>=40){
113+
roman+="XL";
114+
num-=40;
115+
}
116+
117+
while(num>=10){
118+
roman+="X";
119+
num-=10;
120+
}
121+
122+
if(num==9){
123+
roman+="IX";
124+
num-=9;
125+
}
126+
127+
if(num>=5){
128+
roman+="V";
129+
num-=5;
130+
}
131+
if(num==4){
132+
roman+="IV";
133+
num=num-4;
134+
}
135+
while(num>=1){
136+
roman+="I";
137+
num=num-1;
138+
}
139+
return roman;
140+
}
141+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// 121. Best Time to Buy and Sell Stock
2+
// Solved
3+
// Easy
4+
// Topics
5+
// Companies
6+
// You are given an array prices where prices[i] is the price of a given stock on the ith day.
7+
8+
// You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
9+
10+
// Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
11+
12+
13+
14+
// Example 1:
15+
16+
// Input: prices = [7,1,5,3,6,4]
17+
// Output: 5
18+
// Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
19+
// Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
20+
// Example 2:
21+
22+
// Input: prices = [7,6,4,3,1]
23+
// Output: 0
24+
// Explanation: In this case, no transactions are done and the max profit = 0.
25+
26+
27+
// Constraints:
28+
29+
// 1 <= prices.length <= 105
30+
// 0 <= prices[i] <= 104
31+
32+
33+
class Solution {
34+
public:
35+
int maxProfit(vector<int>& prices) {
36+
int profit=0;
37+
int minp=prices[0];
38+
for(int i=0;i<prices.size();i++){
39+
int cost=prices[i]-minp;
40+
profit=max(profit,cost);
41+
minp=min(minp,prices[i]);
42+
}
43+
return profit;
44+
}
45+
};

0 commit comments

Comments
 (0)