<style> html, body, .ui-content { background: #222222; color: #00BFFF; } /* 設定 code 模板 */ .markdown-body code, .markdown-body tt { background-color: #ffffff36; } .markdown-body .highlight pre, .markdown-body pre { color: #ddd; background-color: #00000036; } .hljs-tag { color: #ddd; } .token.operator { background-color: transparent; } /* 設定連結 */ a, .open-files-container li.selected a { color: #89FFF8; } a:hover, .open-files-container li.selected a:hover { color: #89FFF890; } </style> ###### tags: `Leetcode` # 567. Permutation in String ###### Link : https://leetcode.com/problems/permutation-in-string/description/ ## 題目 s1字串各種排列組合的子字串是否存在於s2字串中 ## 程式碼 ```cpp= class Solution { public: bool checkInclusion(string s1, string s2) { const int s1Size = s1.size(), s2Size = s2.size(); if(s1Size > s2Size) return false; vector<int> s1Char(26, 0), temp(26, 0); //s1字元出現次數 for(char &ch : s1) s1Char[ch - 'a']++; //s2中字元出現次數,先放入和s1大小相同的 for(int i = 0;i < s1Size;i++) temp[s2[i] - 'a']++;//s2中字元出現次數 for(int i = s1Size;i < s2Size;i++){ //比較兩者出現次數是否相同 if(s1Char == temp) return true; //在s2中和s1長度相同的子字串向後移一位 temp[s2[i] - 'a']++; temp[s2[i - s1Size] - 'a']--; } if(s1Char == temp) return true; return false; } }; ``` ## Date ### 2023/2/4
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up