-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from Tech-neophyte/main
17/q1 added
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Day 18/q1: Maximum Difference Between Node and Ancestor/Tech-neophyte--c.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## Approach: | ||
<br /> 1. Using dfs find the max value node and min value node in each branch and calculate their diffence. | ||
<br /> 2. Return the max difference. | ||
## Code: | ||
``` | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
int maxAncestorDiff(TreeNode* root) { | ||
return dfs(root,root->val,root->val); | ||
} | ||
int dfs(TreeNode* root,int mini,int maxx){ | ||
if (root==NULL){ | ||
return maxx-mini; | ||
} | ||
mini = std::min(mini, root->val); | ||
maxx = std::max(maxx, root->val); | ||
int left_diff = dfs(root->left, mini, maxx); | ||
int right_diff = dfs(root->right, mini, maxx); | ||
return std::max(left_diff, right_diff); | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Two strings are considered close if you can attain one from the other using the following operations: | ||
Operation 1: Swap any two existing characters. | ||
For example, abcde -> aecdb | ||
Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character. | ||
For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's) | ||
You can use the operations on either string as many times as necessary. | ||
Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise. | ||
|
||
### Example 1: | ||
Input: word1 = "abc", word2 = "bca" | ||
Output: true | ||
Explanation: You can attain word2 from word1 in 2 operations. | ||
Apply Operation 1: "abc" -> "acb" | ||
Apply Operation 1: "acb" -> "bca" | ||
|
||
### Example 2: | ||
Input: word1 = "a", word2 = "aa" | ||
Output: false | ||
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations. | ||
|
||
### Example 3: | ||
Input: word1 = "cabbba", word2 = "abbccc" | ||
Output: true | ||
Explanation: You can attain word2 from word1 in 3 operations. | ||
Apply Operation 1: "cabbba" -> "caabbb" | ||
Apply Operation 2: "caabbb" -> "baaccc" | ||
Apply Operation 2: "baaccc" -> "abbccc" |
40 changes: 40 additions & 0 deletions
40
Day-17/Q1:Determine if two strings are close/Tech-neophyte--c.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
## Approach: | ||
<br />1. Get the freq of each unique character in word1 and word2. | ||
<br />2. Check if all the char present in word1 are present in word2. ( word2 has no unique char). | ||
<br />3. Sort the frequencies and check if the sorted frequencies match. | ||
## C++ code: | ||
``` | ||
class Solution { | ||
public: | ||
bool closeStrings(string word1, string word2) { | ||
if (word1.size() != word2.size()) { | ||
return false; | ||
} | ||
std::unordered_map<char, int> freq1; | ||
for (char c : word1) { | ||
freq1[c]++; | ||
} | ||
std::unordered_map<char, int> freq2; | ||
for (char c : word2) { | ||
freq2[c]++; | ||
} | ||
for (char c : word1) { | ||
if (freq2.find(c) == freq2.end()) { | ||
return false; | ||
} | ||
} | ||
std::vector<int> freqVec1, freqVec2; | ||
for (const auto& pair : freq1) { | ||
freqVec1.push_back(pair.second); | ||
} | ||
for (const auto& pair : freq2) { | ||
freqVec2.push_back(pair.second); | ||
} | ||
std::sort(freqVec1.begin(), freqVec1.end()); | ||
std::sort(freqVec2.begin(), freqVec2.end()); | ||
return freqVec1 == freqVec2; | ||
} | ||
}; | ||
``` |