From 2622c255ed6c8b415a290368385d29cc01bba98f Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Mon, 15 Jan 2024 19:35:39 +0530 Subject: [PATCH 1/3] Create Question.md --- .../Question.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Day-17/Q1:Determine if two strings are close/Question.md diff --git a/Day-17/Q1:Determine if two strings are close/Question.md b/Day-17/Q1:Determine if two strings are close/Question.md new file mode 100644 index 00000000..d85b957c --- /dev/null +++ b/Day-17/Q1:Determine if two strings are close/Question.md @@ -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" From 8cbe42ee2b948bae64130c70f06a23a41bc1f8ff Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Mon, 15 Jan 2024 19:43:16 +0530 Subject: [PATCH 2/3] Day-17/q1 Solution added #399 --- .../Tech-neophyte--c.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Day-17/Q1:Determine if two strings are close/Tech-neophyte--c.md diff --git a/Day-17/Q1:Determine if two strings are close/Tech-neophyte--c.md b/Day-17/Q1:Determine if two strings are close/Tech-neophyte--c.md new file mode 100644 index 00000000..58106b6a --- /dev/null +++ b/Day-17/Q1:Determine if two strings are close/Tech-neophyte--c.md @@ -0,0 +1,40 @@ +## Approach: +
1. Get the freq of each unique character in word1 and word2. +
2. Check if all the char present in word1 are present in word2. ( word2 has no unique char). +
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 freq1; + for (char c : word1) { + freq1[c]++; + } + std::unordered_map freq2; + for (char c : word2) { + freq2[c]++; + } + for (char c : word1) { + if (freq2.find(c) == freq2.end()) { + return false; + } + } + std::vector 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; + } +}; +``` From fa2d45f91e7eabb97ffc13b98b180cdc80f4068d Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Mon, 15 Jan 2024 20:11:23 +0530 Subject: [PATCH 3/3] Solution of day 18-q1 in cpp #396 --- .../Tech-neophyte--c.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Day 18/q1: Maximum Difference Between Node and Ancestor/Tech-neophyte--c.md diff --git a/Day 18/q1: Maximum Difference Between Node and Ancestor/Tech-neophyte--c.md b/Day 18/q1: Maximum Difference Between Node and Ancestor/Tech-neophyte--c.md new file mode 100644 index 00000000..759cc239 --- /dev/null +++ b/Day 18/q1: Maximum Difference Between Node and Ancestor/Tech-neophyte--c.md @@ -0,0 +1,33 @@ +## Approach: +
1. Using dfs find the max value node and min value node in each branch and calculate their diffence. +
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); + } +}; +```