Skip to content

Commit

Permalink
POTD 12-Sep-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
UdaySharmaGitHub committed Sep 13, 2024
1 parent 1fde7b3 commit c370860
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -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<string>& 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';
}();
Original file line number Diff line number Diff line change
@@ -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<string>& 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';
}();
78 changes: 17 additions & 61 deletions GFG/Easy Problems/Finding_middle_element_in_a_linked_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,26 @@ 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
Explanation:
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 <bits/stdc++.h>
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;
Expand All @@ -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*/
Expand All @@ -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
};
Original file line number Diff line number Diff line change
@@ -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<string>& 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';
}();

0 comments on commit c370860

Please sign in to comment.