From 9547da8ed92882d3274e75bd863c8e46a01989a0 Mon Sep 17 00:00:00 2001 From: Tahanima Chowdhury Date: Sun, 10 Oct 2021 23:22:10 +0600 Subject: [PATCH] Add LetCode 1684 cpp, java, cs --- .../Solution.cpp | 27 +++++++++++++++++++ .../Solution.cs | 26 ++++++++++++++++++ .../Solution.java | 26 ++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.cpp create mode 100644 Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.cs create mode 100644 Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.java diff --git a/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.cpp b/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.cpp new file mode 100644 index 0000000..27c7000 --- /dev/null +++ b/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int markCharacters(string &word) { + int marked = 0; + + for (char ch : word) { + int position = ch - 'a'; + marked |= 1 << position; + } + + return marked; + } + + int countConsistentStrings(string allowed, vector& words) { + int markedAllowed = markCharacters(allowed); + int answer = 0; + + for (string word : words) { + int markedWord = markCharacters(word); + + if ((markedWord & markedAllowed) == markedWord) + answer++; + } + + return answer; + } +}; \ No newline at end of file diff --git a/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.cs b/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.cs new file mode 100644 index 0000000..664303c --- /dev/null +++ b/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.cs @@ -0,0 +1,26 @@ +public class Solution { + public int MarkCharacters(String word) { + int marked = 0; + + foreach (char ch in word.ToCharArray()) { + int position = ch - 'a'; + marked |= 1 << position; + } + + return marked; + } + + public int CountConsistentStrings(string allowed, string[] words) { + int markedAllowed = MarkCharacters(allowed); + int answer = 0; + + foreach (String word in words) { + int markedWord = MarkCharacters(word); + + if ((markedWord & markedAllowed) == markedWord) + answer++; + } + + return answer; + } +} \ No newline at end of file diff --git a/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.java b/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.java new file mode 100644 index 0000000..753b3ee --- /dev/null +++ b/Algorithms/Easy/1684_CountTheNumberOfConsistentStrings/Solution.java @@ -0,0 +1,26 @@ +class Solution { + public int markCharacters(String word) { + int marked = 0; + + for (char ch : word.toCharArray()) { + int position = Character.getNumericValue(ch); + marked |= 1 << position; + } + + return marked; + } + + public int countConsistentStrings(String allowed, String[] words) { + int markedAllowed = markCharacters(allowed); + int answer = 0; + + for (String word : words) { + int markedWord = markCharacters(word); + + if ((markedWord & markedAllowed) == markedWord) + answer++; + } + + return answer; + } +} \ No newline at end of file