From c2e77aa8a000e3574f24b136b623a15a117e0863 Mon Sep 17 00:00:00 2001 From: Taj-2005 Date: Sat, 19 Oct 2024 02:44:28 +0530 Subject: [PATCH] Create 567. Permutation in String.cpp --- 567. Permutation in String.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 567. Permutation in String.cpp diff --git a/567. Permutation in String.cpp b/567. Permutation in String.cpp new file mode 100644 index 0000000..e3d7a19 --- /dev/null +++ b/567. Permutation in String.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + bool checkInclusion(string s1, string s2) { + if (s1.length() > s2.length()) return false; + + vector s1Count(26, 0), s2Count(26, 0); + + for (int i = 0; i < s1.length(); ++i) { + s1Count[s1[i] - 'a']++; + s2Count[s2[i] - 'a']++; + } + + for (int i = 0; i < s2.length() - s1.length(); ++i) { + if (s1Count == s2Count) return true; + s2Count[s2[i] - 'a']--; + s2Count[s2[i + s1.length()] - 'a']++; + } + + return s1Count == s2Count; + } +};