aboutsummaryrefslogtreecommitdiff
path: root/other/permutation_in_string.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/permutation_in_string.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/permutation_in_string.cpp')
-rw-r--r--other/permutation_in_string.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/other/permutation_in_string.cpp b/other/permutation_in_string.cpp
new file mode 100644
index 0000000..f4e09f3
--- /dev/null
+++ b/other/permutation_in_string.cpp
@@ -0,0 +1,47 @@
+// 567. Permutation in String
+#include "leetcode.h"
+
+/*
+ * given two strings 's1 s2' return true if 's2' contains a permutation of 's1'
+ * or false otherwise. in other words, return true if one of 's1's permutations
+ * is in the substring of 's2'
+ */
+
+class Solution {
+public:
+ bool checkInclusion(string s1, string s2) {
+ if (s2.size() < s1.size())
+ return false;
+ vector<int> freq1(26, 0), freq2(26, 0);
+ for (char c : s1)
+ freq1[c - 'a']++;
+ int i = 0, j = 0;
+ while (j < s2.size()) {
+ freq2[s2[j] - 'a']++;
+ if (j - i + 1 == s1.size())
+ if (cmp(freq1, freq2))
+ return true;
+ if (j - i + 1 < s1.size())
+ j++;
+ else {
+ freq2[s2[i] - 'a']--;
+ i++, j++;
+ }
+ }
+ return false;
+ }
+
+private:
+ bool cmp(vector<int> a, vector<int> b) {
+ for (int i = 0; i < 26; i++)
+ if (a[i] != b[i])
+ return false;
+ return true;
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.checkInclusion("ab", "eidbaooo") << endl; // expect: 1
+ cout << obj.checkInclusion("ab", "eidboaoo") << endl; // expect: 0
+}