From c370860ac7f8b7c3d37e482b4415def2842450da Mon Sep 17 00:00:00 2001 From: Uday Date: Fri, 13 Sep 2024 19:11:43 +0530 Subject: [PATCH] POTD 12-Sep-2024 --- ...Count_the_Number_of_Consistent_Strings.cpp | 52 +++++++++++++ ...Count_the_Number_of_Consistent_Strings.cpp | 52 +++++++++++++ ...inding_middle_element_in_a_linked_list.cpp | 78 ++++--------------- ...Count_the_Number_of_Consistent_Strings.cpp | 52 +++++++++++++ 4 files changed, 173 insertions(+), 61 deletions(-) create mode 100644 08_String/Problems/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp create mode 100644 09_Bit_Manipulation/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp create mode 100644 Leet Code Problems/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp diff --git a/08_String/Problems/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp b/08_String/Problems/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp new file mode 100644 index 0000000..db3e723 --- /dev/null +++ b/08_String/Problems/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp @@ -0,0 +1,52 @@ +/* +1684. Count the Number of Consistent Strings +You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed. +Return the number of consistent strings in the array words. +Example 1: +Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"] +Output: 2 +Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'. +Example 2: +Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"] +Output: 7 +Explanation: All strings are consistent. +Example 3: +Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"] +Output: 4 +Explanation: Strings "cc", "acd", "ac", and "d" are consistent. +Constraints: +1 <= words.length <= 104 +1 <= allowed.length <= 26 +1 <= words[i].length <= 10 +The characters in allowed are distinct. +words[i] and allowed contain only lowercase English letters. +*/ +class Solution { +public: + int countConsistentStrings(string& allowed, vector& words) { + bitset<26> ASet=0; + for(char c: allowed) + ASet[c-'a']=1; + int cnt=0; + for(string& w: words){ + bool consistent=1; + for(char c: w){ + if (ASet[c-'a']==0){ + consistent=0; + break; + } + } + cnt+=consistent; + } + return cnt; + } +}; + + + +auto init = []() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + return 'c'; +}(); \ No newline at end of file diff --git a/09_Bit_Manipulation/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp b/09_Bit_Manipulation/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp new file mode 100644 index 0000000..db3e723 --- /dev/null +++ b/09_Bit_Manipulation/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp @@ -0,0 +1,52 @@ +/* +1684. Count the Number of Consistent Strings +You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed. +Return the number of consistent strings in the array words. +Example 1: +Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"] +Output: 2 +Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'. +Example 2: +Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"] +Output: 7 +Explanation: All strings are consistent. +Example 3: +Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"] +Output: 4 +Explanation: Strings "cc", "acd", "ac", and "d" are consistent. +Constraints: +1 <= words.length <= 104 +1 <= allowed.length <= 26 +1 <= words[i].length <= 10 +The characters in allowed are distinct. +words[i] and allowed contain only lowercase English letters. +*/ +class Solution { +public: + int countConsistentStrings(string& allowed, vector& words) { + bitset<26> ASet=0; + for(char c: allowed) + ASet[c-'a']=1; + int cnt=0; + for(string& w: words){ + bool consistent=1; + for(char c: w){ + if (ASet[c-'a']==0){ + consistent=0; + break; + } + } + cnt+=consistent; + } + return cnt; + } +}; + + + +auto init = []() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + return 'c'; +}(); \ No newline at end of file diff --git a/GFG/Easy Problems/Finding_middle_element_in_a_linked_list.cpp b/GFG/Easy Problems/Finding_middle_element_in_a_linked_list.cpp index 814ada7..837a672 100644 --- a/GFG/Easy Problems/Finding_middle_element_in_a_linked_list.cpp +++ b/GFG/Easy Problems/Finding_middle_element_in_a_linked_list.cpp @@ -4,16 +4,13 @@ The task is to find the middle of the linked list. For example, if the linked li 1-> 2->3->4->5, then the middle node of the list is 3. If there are two middle nodes(in case, when N is even), print the second middle element. For example, if the linked list given is 1->2->3->4->5->6, then the middle node of the list is 4. - Example 1: - Input: LinkedList: 1->2->3->4->5 Output: 3 Explanation: Middle of linked list is 3. Example 2: - Input: LinkedList: 2->4->6->7->5->1 Output: 7 @@ -21,40 +18,12 @@ Output: 7 Middle of linked list is 7. Your Task: The task is to complete the function getMiddle() which takes a head reference as the only argument and should return the data at the middle node of the linked list. - Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). - Constraints: 1 <= N <= 5000 */ -//{ Driver Code Starts -//Initial template for C++ - -#include -using namespace std; -struct Node -{ - int data; - struct Node* next; - - Node(int x){ - data = x; - next = NULL; - } -}; -void printList(Node* node) -{ - while (node != NULL) { - cout << node->data <<" "; - node = node->next; - } - cout<<"\n"; -} - - -// } Driver Code Ends /* Link list Node struct Node { int data; @@ -66,6 +35,22 @@ struct Node { } }; */ +class Solution { + public: + /* Should return data of middle node. If linked list is empty, then -1 */ + int getMiddle(Node* head) { + // code here + if(!head || !head->next) return head->data; + Node* slow = head , *fast = head->next; + while(fast){ + slow = slow ->next; + fast = fast->next; + if(fast) fast =fast->next; + } + return slow->data; + } +}; + class Solution{ public: /* Should return data of middle node. If linked list is empty, then -1*/ @@ -90,33 +75,4 @@ class Solution{ } return temp->data; } -}; - - -//{ Driver Code Starts. - -int main() { - //code - int t; - cin>>t; - while(t--){ - int N; - cin>>N; - int data; - cin>>data; - struct Node *head = new Node(data); - struct Node *tail = head; - for (int i = 0; i < N-1; ++i) - { - cin>>data; - tail->next = new Node(data); - tail = tail->next; - } - - Solution ob; - cout << ob.getMiddle(head) << endl; - } - return 0; -} - -// } Driver Code Ends \ No newline at end of file +}; \ No newline at end of file diff --git a/Leet Code Problems/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp b/Leet Code Problems/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp new file mode 100644 index 0000000..db3e723 --- /dev/null +++ b/Leet Code Problems/Easy/1684_Count_the_Number_of_Consistent_Strings.cpp @@ -0,0 +1,52 @@ +/* +1684. Count the Number of Consistent Strings +You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed. +Return the number of consistent strings in the array words. +Example 1: +Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"] +Output: 2 +Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'. +Example 2: +Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"] +Output: 7 +Explanation: All strings are consistent. +Example 3: +Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"] +Output: 4 +Explanation: Strings "cc", "acd", "ac", and "d" are consistent. +Constraints: +1 <= words.length <= 104 +1 <= allowed.length <= 26 +1 <= words[i].length <= 10 +The characters in allowed are distinct. +words[i] and allowed contain only lowercase English letters. +*/ +class Solution { +public: + int countConsistentStrings(string& allowed, vector& words) { + bitset<26> ASet=0; + for(char c: allowed) + ASet[c-'a']=1; + int cnt=0; + for(string& w: words){ + bool consistent=1; + for(char c: w){ + if (ASet[c-'a']==0){ + consistent=0; + break; + } + } + cnt+=consistent; + } + return cnt; + } +}; + + + +auto init = []() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + return 'c'; +}(); \ No newline at end of file