From 2f40e5c2c5a033dd4e2286b9987738dbf00e7061 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 15 Sep 2025 07:26:12 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1935 --- .../README.md | 48 ++++++++++++------- .../README_EN.md | 48 ++++++++++++------- .../Solution.cpp | 25 +++------- .../Solution.cs | 20 ++++++++ 4 files changed, 86 insertions(+), 55 deletions(-) create mode 100644 solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cs diff --git a/solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md b/solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md index b81d17f99095b..b28214649cf98 100644 --- a/solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md +++ b/solution/1900-1999/1935.Maximum Number of Words You Can Type/README.md @@ -116,12 +116,14 @@ class Solution { public: int canBeTypedWords(string text, string brokenLetters) { bool s[26]{}; - for (char& c : brokenLetters) { + for (char c : brokenLetters) { s[c - 'a'] = true; } int ans = 0; - for (auto& w : split(text, ' ')) { - for (char& c : w) { + stringstream ss(text); + string w; + while (ss >> w) { + for (char c : w) { if (s[c - 'a']) { --ans; break; @@ -131,21 +133,6 @@ public: } return ans; } - - vector split(const string& s, char c) { - vector ans; - string t; - for (char d : s) { - if (d == c) { - ans.push_back(t); - t.clear(); - } else { - t.push_back(d); - } - } - ans.push_back(t); - return ans; - } }; ``` @@ -217,6 +204,31 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public int CanBeTypedWords(string text, string brokenLetters) { + bool[] s = new bool[26]; + foreach (char c in brokenLetters) { + s[c - 'a'] = true; + } + int ans = 0; + string[] words = text.Split(' '); + foreach (string w in words) { + foreach (char c in w) { + if (s[c - 'a']) { + --ans; + break; + } + } + ++ans; + } + return ans; + } +} +``` + diff --git a/solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md b/solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md index d1d843f3f838a..dbdf37067082d 100644 --- a/solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md +++ b/solution/1900-1999/1935.Maximum Number of Words You Can Type/README_EN.md @@ -117,12 +117,14 @@ class Solution { public: int canBeTypedWords(string text, string brokenLetters) { bool s[26]{}; - for (char& c : brokenLetters) { + for (char c : brokenLetters) { s[c - 'a'] = true; } int ans = 0; - for (auto& w : split(text, ' ')) { - for (char& c : w) { + stringstream ss(text); + string w; + while (ss >> w) { + for (char c : w) { if (s[c - 'a']) { --ans; break; @@ -132,21 +134,6 @@ public: } return ans; } - - vector split(const string& s, char c) { - vector ans; - string t; - for (char d : s) { - if (d == c) { - ans.push_back(t); - t.clear(); - } else { - t.push_back(d); - } - } - ans.push_back(t); - return ans; - } }; ``` @@ -218,6 +205,31 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public int CanBeTypedWords(string text, string brokenLetters) { + bool[] s = new bool[26]; + foreach (char c in brokenLetters) { + s[c - 'a'] = true; + } + int ans = 0; + string[] words = text.Split(' '); + foreach (string w in words) { + foreach (char c in w) { + if (s[c - 'a']) { + --ans; + break; + } + } + ++ans; + } + return ans; + } +} +``` + diff --git a/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cpp b/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cpp index d74caeb8f107f..cc2503e26dec3 100644 --- a/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cpp +++ b/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cpp @@ -2,12 +2,14 @@ class Solution { public: int canBeTypedWords(string text, string brokenLetters) { bool s[26]{}; - for (char& c : brokenLetters) { + for (char c : brokenLetters) { s[c - 'a'] = true; } int ans = 0; - for (auto& w : split(text, ' ')) { - for (char& c : w) { + stringstream ss(text); + string w; + while (ss >> w) { + for (char c : w) { if (s[c - 'a']) { --ans; break; @@ -17,19 +19,4 @@ class Solution { } return ans; } - - vector split(const string& s, char c) { - vector ans; - string t; - for (char d : s) { - if (d == c) { - ans.push_back(t); - t.clear(); - } else { - t.push_back(d); - } - } - ans.push_back(t); - return ans; - } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cs b/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cs new file mode 100644 index 0000000000000..6766eef70e8c9 --- /dev/null +++ b/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.cs @@ -0,0 +1,20 @@ +public class Solution { + public int CanBeTypedWords(string text, string brokenLetters) { + bool[] s = new bool[26]; + foreach (char c in brokenLetters) { + s[c - 'a'] = true; + } + int ans = 0; + string[] words = text.Split(' '); + foreach (string w in words) { + foreach (char c in w) { + if (s[c - 'a']) { + --ans; + break; + } + } + ++ans; + } + return ans; + } +}