 3106. Lexicographically Smallest String After Operations With Constraint | Adhoc | Strings
🧠 # IntuitionOkay, so you’re handed a string s, and you’re allowed to reduce the characters in it to make it lexicographically smaller. But—there’s a catch—you


### 🧠 # Intuition

Okay, so you’re handed a string `s`, and you’re allowed to **reduce the characters** in it to make it lexicographically smaller. But—there’s a catch—you can only **spend `k` units total**. 

Each character can only be reduced *towards* `'a'` by using your `k` points. For example:
- `'d' -> 'a'` costs 3
- `'c' -> 'a'` costs 2
- `'b' -> 'a'` costs 1

Your goal is to make the string as small (lexicographically) as possible by using those k points wisely. Like playing a game where you downgrade letters, but you don’t want to overspend.

---

### ⚙️ # Approach

Let’s think of it step-by-step, like we’re solving a puzzle:

1. **Loop through each character** of the string.
2. For each character, ask yourself:
   > “How much would it cost to turn this guy into an `'a'`?”
   
   There are two options:
   - Go **forward** from `s[i]` to `'z'` and then wrap around to `'a'`
   - OR just go **backward** directly to `'a'`

   But here’s the twist in the logic:
   Since we’re only allowed to **decrease** letters (not cycle), we don’t need to think about wrapping around. So the actual cost is:
   ```cpp
   min('z' - s[i] + 1, s[i] - 'a')
   ```
   Actually... wait — this line looks like it allows **wrapping around**, which isn’t required in this problem unless explicitly stated. Usually, it’s just:
   ```cpp
   cost = s[i] - 'a';
   ```
   Because we can only move **towards `'a'`**, not past it.

   Assuming that’s clarified...

3. If `cost <= k`, spend it and change the character to `'a'`.
4. If `cost > k`, we can’t afford a full downgrade. So we just reduce as much as we can by subtracting `k` from the character and setting `k = 0`.

---

### 📊 # Complexity

- **Time Complexity:**  
  We’re visiting each character exactly once and doing constant-time operations — so:
  $$O(n)$$ where *n* is the length of the string.

- **Space Complexity:**  
  We're building a new string, same length as input → 
  $$O(n)$$ space for the answer.

---

### 💻 # Code Cleanup & Comments
Let’s rewrite it with super-clear comments for logic clarity:

```cpp
class Solution {
public:
    string getSmallestString(string s, int k) {
        string ans = "";
        
        for (int i = 0; i < s.size(); i++) {
            // How far is this char from 'a'?
            int cost = s[i] - 'a';
            
            // If we can afford to make it 'a', do it
            if (cost <= k) {
                k -= cost;
                ans += 'a';
            } else {
                // Reduce it as much as possible
                ans += s[i] - k;
                k = 0; // all used up
            }
        }
        
        return ans;
    }
};
```

---

### 🧩 Deep Logic Vibes

This is basically a **greedy approach**—you’re trying to make **every character as small as possible**, starting from the front. Why? Because the front characters matter *more* in lexicographical order. 

['C++']