LeetCode, GeeksForGeeks Problem of the day solution
前往频道在 Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
显示更多1 250
订阅者
+224 小时
+147 天
+2930 天
帖子存档
class Solution {
public:
bool closeStrings(string word1, string word2) {
if (word1.size() != word2.size()) {
return false;
}
int a[26] = {0}, b[26] = {0}, mask1 = 0, mask2 = 0;
for (int i = 0; i < word1.size(); i++) {
a[word1[i] - 'a']++;
b[word2[i] - 'a']++;
mask1 |= 1 << (word1[i] - 'a');
mask2 |= 1 << (word2[i] - 'a');
};
if (mask1 != mask2) {
return false;
}
sort(begin(a), end(a));
sort(begin(b), end(b));
for (int i = 0; i < 26; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
};
class Solution
{
public:
vector repeatedRows(vector> &matrix, int M, int N)
{
// Your code here
vector res;
unordered_set set;
int r=M;
int c=N;
for(int i=0;i
class Solution {
public:
int minSteps(string s, string t) {
int count =0;
int num1=0;
int num2=0;
sort(s.begin(),s.end());
sort(t.begin(),t.end());
while(num1t[num2])
num2++;
}
return s.size()-count ;
}
};
class Solution
{
public:
Node* insertionSort(struct Node* head)
{
if(!head || !head->next) return head;
Node* dummy=new Node(-1);
dummy->next=head;
Node* temp=head;
Node* it=head->next;
head->next=NULL;
while(it){
Node* curr=it;
Node* next=curr->next;
curr->next=NULL;
Node* prev=dummy;
while(temp){
if(temp->data >= curr->data) break;
temp=temp->next;
prev=prev->next;
}
prev->next=curr;
curr->next=temp;
temp=dummy->next;
it=next;
}
return dummy->next;
}
};
class Solution {
public:
bool vowelCheck(char ch){
if(ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u' or ch=='A' or ch=='E' or
ch=='I' or ch=='O' or ch=='U'){
return true;
}
return false;
}
bool halvesAreAlike(string s) {
int cnt=0;
int n=s.size();
for(int i=0;i
class Solution
{
public:
// Function to reverse first k elements of a queue.
queue modifyQueue(queue q, int k) {
int n=q.size();
vector arr(n, 0);
int j=k-1;
for(int i=0;i=0){
arr[j]=q.front();
j--;
}
else{
arr[i] = q.front();
}
q.pop();
}
for(int i=0;i
class Solution {
public:
int diff = INT_MIN; // we want max difference
void getMax(TreeNode *root, int mx, int mi){
if(!root) return;
// track minimum and maximum while going through a sequence or path
mx = max(mx, root->val);
mi = min(mi, root->val);
diff = max(diff, mx - mi); // store maximum difference
getMax(root->left,mx,mi);
getMax(root->right,mx,mi);
}
int maxAncestorDiff(TreeNode* root) {
if(!root) return 0;
int mx = INT_MIN, mi = INT_MAX;
getMax(root,mx,mi);
return diff;
}
};
class Solution {
public:
string removeKdigits(string s, int k) {
int n=s.size();
vector<int>v(10,0);
stack<char>st;
for(int i=0;i<n;i++)
{
while(!st.empty()&&s[i]<st.top()&&k>0)
{
st.pop();
k--;
}
if(st.empty()&&s[i]=='0')
continue;
st.push(s[i]);
}
while(!st.empty()&&k--)
{
st.pop();
}
if(st.empty())
{
return "0";
}
string ans="";
while(!st.empty())
{
ans+=st.top();
st.pop();
}
reverse(ans.begin(),ans.end());
return ans;
}
};
class Solution {
public:
void adjListCreating(unordered_map> &adj, TreeNode *root){
queue que;
que.push(root);
while(!que.empty()){
TreeNode *temp = que.front();
que.pop();
if(temp->left){
adj[temp->val].push_back(temp->left->val);
adj[temp->left->val].push_back(temp->val);
que.push(temp->left);
}
if(temp->right){
adj[temp->val].push_back(temp->right->val);
adj[temp->right->val].push_back(temp->val);
que.push(temp->right);
}
}
}
int amountOfTime(TreeNode* root, int start) {
unordered_map> adj;
adjListCreating(adj, root);
unordered_map visited;
for(auto i : adj){
visited[i.first] = false;
}
int maxVal = 0;
queue> que;
que.push({start, 0});
visited[start] = true;
while(!que.empty()){
pair temp = que.front();
que.pop();
for(auto i : adj[temp.first]){
if(!visited[i]){
visited[i] = true;
que.push({i, temp.second+1});
maxVal = max(maxVal, temp.second + 1);
}
}
}
return maxVal;
}
};
class Solution{
public:
int longSubarrWthSumDivByK(int arr[], int n, int k)
{
map m;
m[0]=-1;
int sum=0;
int rem;
int largest=0;
for(int i=0;isecond)>largest) largest=i-itr->second;
}
else m[rem]=i;
}
return largest;
}
};
