Shadab Ali

Jul 10, 2025 • 3 min read

Easy Code Must Practice Leetcode

Here’s a collection of common yet essential LeetCode problems that we’ve solved with clean logic and explanations. These cover strings, arrays, trees, maps, and custom data structures — perfect for building problem-solving muscle.


✅ 1. Is Subsequence

Problem: Check if string s is a subsequence of t.

Key Logic:

Use two pointers: advance both if characters match; otherwise, move pointer of t.

bool isSubsequence(string s, string t) {
    int j = 0;
    for (int i = 0; i < t.size(); i++)
        if (j < s.size() && s[j] == t[i]) j++;
    return j == s.size();
}

✅ 2. Container With Most Water

Problem: Find two lines that form the container with maximum water.

Key Logic:

Two-pointer approach from both ends, always move the smaller height.

int maxArea(vector<int>& height) {
    int i = 0, j = height.size() - 1, area = 0;
    while (i < j) {
        area = max(area, min(height[i], height[j]) * (j - i));
        (height[i] < height[j]) ? i++ : j--;
    }
    return area;
}

✅ 3. Pascal’s Triangle – Get Row

Problem: Return the nth row of Pascal's Triangle.

Key Logic:

Build the triangle row-by-row.

vector<int> getRow(int rowIndex) {
    vector<int> row = {1};
    for (int i = 1; i <= rowIndex; i++) {
        vector<int> temp = {1};
        for (int j = 0; j < row.size() - 1; j++)
            temp.push_back(row[j] + row[j + 1]);
        temp.push_back(1);
        row = temp;
    }
    return row;
}

✅ 4. Reverse Bits

Problem: Reverse bits of a 32-bit unsigned integer.

Key Logic:

Shift and add bits one by one.

uint32_t reverseBits(uint32_t n) {
    uint32_t res = 0;
    for (int i = 0; i < 32; i++) {
        res <<= 1;
        res |= (n & 1);
        n >>= 1;
    }
    return res;
}

✅ 5. Happy Number

Problem: Determine if a number eventually becomes 1 by repeatedly summing the square of its digits.

Key Logic:

Use set to detect loops.

bool isHappy(int n) {
    unordered_set<int> seen;
    while (n != 1 && !seen.count(n)) {
        seen.insert(n);
        int sum = 0;
        while (n) {
            int d = n % 10;
            sum += d * d;
            n /= 10;
        }
        n = sum;
    }
    return n == 1;
}

✅ 6. Remove Linked List Elements

Problem: Remove all nodes from the list that have a given value.

Key Logic:

Handle edge cases (head node removal), then loop.

ListNode* removeElements(ListNode* head, int val) {
    while (head && head->val == val) head = head->next;
    ListNode* curr = head;
    while (curr && curr->next) {
        if (curr->next->val == val)
            curr->next = curr->next->next;
        else
            curr = curr->next;
    }
    return head;
}

✅ 7. Isomorphic Strings

Problem: Determine if two strings can be mapped character-by-character bijectively.

Key Logic:

Map s[i] → t[i] and ensure mapping is consistent and one-to-one.

bool isIsomorphic(string s, string t) {
    unordered_map<char, char> m1, m2;
    for (int i = 0; i < s.size(); i++) {
        if (m1.count(s[i]) && m1[s[i]] != t[i]) return false;
        if (m2.count(t[i]) && m2[t[i]] != s[i]) return false;
        m1[s[i]] = t[i];
        m2[t[i]] = s[i];
    }
    return true;
}

✅ 8. Invert Binary Tree

Problem: Mirror a binary tree.

Key Logic:

Swap left and right at every node recursively.

TreeNode* invertTree(TreeNode* root) {
    if (!root) return nullptr;
    swap(root->left, root->right);
    invertTree(root->left);
    invertTree(root->right);
    return root;
}

✅ 9. Binary Tree Paths

Problem: Return all root-to-leaf paths in a binary tree.

Key Logic:

DFS traversal and backtracking.

void dfs(TreeNode* root, vector<string>& res, string path) {
    if (!root) return;
    path += "->" + to_string(root->val);
    if (!root->left && !root->right)
        res.push_back(path.substr(2));
    dfs(root->left, res, path);
    dfs(root->right, res, path);
}

✅ 10. Queue using Stacks

Problem: Implement a queue using two stacks.

Key Logic:

Reverse stack every time in push() to keep front on top.

class MyQueue {
    stack<int> st1, st2;
public:
    void push(int x) {
        while (!st1.empty()) {
            st2.push(st1.top());
            st1.pop();
        }
        st1.push(x);
        while (!st2.empty()) {
            st1.push(st2.top());
            st2.pop();
        }
    }

    int pop() { int x = st1.top(); st1.pop(); return x; }
    int peek() { return st1.top(); }
    bool empty() { return st1.empty(); }
};

🧠 Final Words

This collection covers a variety of interview-style problems using clean and optimal C++ solutions, with a strong focus on clarity, edge-case handling, and real-world patterns.

Join Shadab on Peerlist!

Join amazing folks like Shadab and thousands of other builders on Peerlist.

peerlist.io/

It’s available... this username is available! 😃

Claim your username before it's too late!

This username is already taken, you’re a little late.😐

0

2

0