This repository contains all the leetcode must solve problems.
Before jumping into coding:
- Read the problem twice.
- Identify input and output formats.
- Clarify constraints (e.g., input size, time limits).
- Restate the problem in your own words.
- Think of examples manually.
Key Questions to Ask:
- What exactly do I need to find/return?
- Are the inputs sorted, unique, or repetitive?
- Do I care about time/memory efficiency?
| Problem Type | Hints for Data Structures/Algorithms | 
|---|---|
| Searching | Binary Search, Hash Maps, Trees | 
| Sorting | Merge Sort, Quick Sort | 
| String Manipulation | Two Pointers, Stack, Sliding Window | 
| Dynamic Programming | Arrays, Recursion + Memoization | 
| Graph Problems | BFS, DFS, Dijkstra, Union-Find | 
| Range Queries | Segment Trees, Fenwick Trees | 
| Combinatorics | Recursion, Backtracking | 
| Data Structure | Use When You Need To... | 
|---|---|
| Array/List | Store a sequence; access by index | 
| HashMap | Quick lookup by key (O(1) avg. time) | 
| Set | Store unique items quickly | 
| Stack | LIFO behavior; backtracking, parsing | 
| Queue | FIFO behavior; BFS, task scheduling | 
| Heap | Get min/max quickly; priority queues | 
| Graph | Represent connections/networks | 
| Tree | Hierarchical data, faster search (BST, Trie) | 
| Pattern | Description | Common Problems | 
|---|---|---|
| Two Pointers | Iterate from both ends | Palindromes, Sorted Arrays | 
| Sliding Window | Subarrays with conditions | Max sum subarray | 
| Binary Search | Divide and conquer search | Search in rotated array | 
| DFS/BFS | Graph/tree traversal | Path finding | 
| Backtracking | Try all options and undo | Sudoku, N-Queens | 
| Greedy | Choose best option locally | Activity selection | 
| Dynamic Prog. | Break into subproblems | Fibonacci, Knapsack | 
- Input: "leetcode"
- Output: "l"
- Understand: Return the first character that appears only once.
- Example: "loveleetcode"β"v"
- Constraints: Only lowercase letters.
- Approach: Use a HashMap to count frequencies.
- Traverse string, count frequency.
- Traverse again, return first character with frequency 1.
#include <iostream>
#include <unordered_map>
#include <string>
char firstNonRepeatingChar(const std::string& s) {
    std::unordered_map<char, int> freq;
    // Count frequencies
    for (char ch : s) {
        freq[ch]++;
    }
    // Find first non-repeating
    for (char ch : s) {
        if (freq[ch] == 1) {
            return ch;
        }
    }
    return '\0'; // or a special char like '_' if no such character
}
int main() {
    std::string input = "loveleetcode";
    char result = firstNonRepeatingChar(input);
    if (result != '\0')
        std::cout << "First non-repeating character: " << result << std::endl;
    else
        std::cout << "No non-repeating character found." << std::endl;
    return 0;
}This README serves as a cheat-sheet and includes:
- Problem understanding checklist β
- Classification of problem types π
- Key data structures and patterns π§°
- Real example solved step-by-step with C++ π»
| 0020-valid-parentheses | 
| 1078-remove-outermost-parentheses | 
| 1737-maximum-nesting-depth-of-the-parentheses | 
| 0013-roman-to-integer | 
| 0048-rotate-image | 
| 0189-rotate-array | 
| 0268-missing-number | 
| 2032-largest-odd-number-in-string | 
| 0011-container-with-most-water | 
| 2032-largest-odd-number-in-string | 
| 0014-longest-common-prefix | 
| 0812-rotate-string | 
| 0015-3sum | 
| 0049-group-anagrams | 
| 0075-sort-colors | 
| 0088-merge-sorted-array | 
| 0169-majority-element | 
| 0217-contains-duplicate | 
| 0242-valid-anagram | 
| 0268-missing-number | 
| 0451-sort-characters-by-frequency | 
| 0451-sort-characters-by-frequency | 
| 0451-sort-characters-by-frequency | 
| 0169-majority-element | 
| 0451-sort-characters-by-frequency | 
| 1890-sum-of-beauty-of-all-substrings | 
| 0005-longest-palindromic-substring | 
| 0053-maximum-subarray | 
| 0118-pascals-triangle | 
| 0121-best-time-to-buy-and-sell-stock | 
| 0152-maximum-product-subarray | 
| 0136-single-number | 
| 0268-missing-number | 
| 0053-maximum-subarray | 
| 0169-majority-element | 
| 0048-rotate-image | 
| 0054-spiral-matrix | 
| 0073-set-matrix-zeroes | 
| 0054-spiral-matrix | 
| 0238-product-of-array-except-self | 
| 0560-subarray-sum-equals-k | 
| 0021-merge-two-sorted-lists | 
| 0021-merge-two-sorted-lists | 
| 0003-longest-substring-without-repeating-characters | 
| 0076-minimum-window-substring | 
| 0424-longest-repeating-character-replacement |