Strings, grids, and pattern matching
s, reverse only all the vowels in the string and return it. The vowels are a, e, i, o, and u, and they can appear in both lower and upper cases, more than once.
The key phrase: "reverse only all the vowels". Consonants don't move. Only the vowels swap positions with each other.
For "hello" the vowels are e (index 1) and o (index 4). Swap them to get "holle". The h, l, l stay put.
Also note: vowels can be upper or lower case. Handle both a and A.
We need to pair up vowels from opposite ends and swap them. First vowel from the left swaps with the first from the right, and so on inward.
Use two pointers, one at the start, one at the end. Each skips consonants and stops on a vowel. When both land on vowels, swap and move inward.
Store all 10 vowel characters in a set so each "is this a vowel?" check is instant.
⚙ Knowledge Hub: Using SetsaeiouAEIOU) in a set so you can check membership instantlyleft = 0, right = length - 1while loops to advance left forward and right backward, skipping anything that isn't a vowel. Always guard with left < rightleft++, right--)A stack is last-in-first-out. If you push all the vowels onto a stack in left-to-right order, popping them gives you the vowels in reverse order, which is exactly what we need.
This is a different way to think about the same problem. Instead of swapping in place with two pointers, we collect, reverse via the stack, then place them back.
⚙ Knowledge Hub: Using StacksaeiouAEIOU) in a set for instant membership checkss and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order. No two characters may map to the same character, but a character may map to itself.
Key phrase: "characters in s can be replaced to get t". Every character in s must consistently map to the same character in t. For "egg" to "add": e always maps to a, g always maps to d.
Critical detail: "no two characters may map to the same character". It's a one-to-one relationship. If e->a, nothing else can map to a.
We need to track which character maps to which. Use a dictionary to store the mapping and check it on each new pair.
⚙ Knowledge Hub: Using DictionariesOne map (s->t) alone misses cases. Try s = "badc", t = "baba". A forward map accepts it, but both b and d map to b. That violates the one-to-one rule.
Add a reverse map (t->s). Now every mapping is checked in both directions.
s->t) and one for the reverse (t->s)s and the character at the same position in ts character must still map to the same t character as before, and vice versa. Any mismatch means the strings aren't isomorphic, so return falseWe have a big grid of digit strings and a smaller pattern. The question: does this smaller block appear anywhere inside the larger one?
The pattern has multiple rows, so you need to find a position where every row of the pattern matches the corresponding section of the grid at the same time.
The pattern can only start where there's room for it to fit. If the grid has R rows and the pattern has r rows, valid starting rows are 0 through R - r. Same for columns.
Always calculate valid bounds first. This prevents off-by-one errors.
rg, cg (grid rows/cols) and rp, cp (pattern rows/cols)i if i + rp <= rg, and column j if j + cp <= cg. Use two nested loops over these valid ranges(i, j), run a third loop k = 0..rp-1 comparing one pattern row at a time: extract a substring of length cp from G[i+k] starting at column j, and check if it equals P[k]found = false) and break out early since there's no need to check the restrp rows matched, you've found the pattern, so return "YES""NO"n - board size (n×n)k - number of obstaclesr_q, c_q - queen's position (1-indexed)obstacles - list of [row, col] positionsThe queen attacks in 8 directions: up, down, left, right, and four diagonals. She moves any number of squares until she hits the board edge or an obstacle.
Obstacles block her. She stops before the obstacle, not on it. Count every valid square she can reach across all 8 directions.
For each step the queen takes, we need to check: "is there an obstacle here?" Scanning the obstacle list every time is too slow for large boards.
Put all obstacle positions in a set. Each lookup becomes instant. Sets use the same hash-based approach as dictionaries, just keys without values.
⚙ Knowledge Hub: Using Sets(row, col) to a set. This gives you O(1) "is there an obstacle here?" checks later(-1,0) up, (1,0) down, (0,1) right, (0,-1) left, and the four diagonals (-1,1), (-1,-1), (1,1), (1,-1)(dr, dc): start at the queen's position and use a while true loop, adding dr/dc each step to move outwardr < 1 or r > n or same for c). If so, break. (2) is it in the obstacle set? If so, break. Otherwise increment a counter.