Загрузка...

Leetcode permutation in string(567) #dsa #coding #hashmap #python #lc567 #leetcode #slidingwindow

🔥 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: 𝗣𝗲𝗿𝗺𝘂𝘁𝗮𝘁𝗶𝗼𝗻 𝗶𝗻 𝗦𝘁𝗿𝗶𝗻𝗴 (𝗦𝗹𝗶𝗱𝗶𝗻𝗴 𝗪𝗶𝗻𝗱𝗼𝘄)

Check if string s2 contains any permutation of string s1.

🧠 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 𝗨𝘀𝗲𝗱
Sliding Window + Frequency Map

This problem uses:
• Variable size sliding window
• Hash map comparison
• Anagram detection

If two strings have the same character frequencies, they are permutations.

⚙️ 𝗛𝗼𝘄 𝗧𝗵𝗲 𝗔𝗹𝗴𝗼𝗿𝗶𝘁𝗵𝗺 𝗪𝗼𝗿𝗸𝘀

➊ Build a frequency map of s1
Store how many times each character appears.
Example: s1 = "ab" → {a:1, b:1}

➋ Slide a window over s2
Move a window of size equal to length of s1 across s2.

For each step:
• Add the new character to the window
• Remove the leftmost character when the window grows too large

➌ Compare frequency maps
If the window frequency matches s1 frequency → permutation found → return True.

🧪 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
s1 = "ab"
s2 = "eidbaooo"

Window "ba" matches frequency → True

⏱️ 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆
Time Complexity: O(n) — each character processed once
Space Complexity: O(1) — limited alphabet size

🎯 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆𝘀
• Sliding window avoids recomputing counts
• Frequency maps detect permutations efficiently
• This pattern appears in many coding interviews

🚀 𝗥𝗲𝗹𝗮𝘁𝗲𝗱 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲
• Find All Anagrams in a String
• Longest Substring Without Repeating Characters
• Minimum Window Substring

If you still brute force after learning this pattern… that’s a skill issue 😄

Видео Leetcode permutation in string(567) #dsa #coding #hashmap #python #lc567 #leetcode #slidingwindow канала faangcracker
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять