Easy In-place C++ Solution | No Extra Space/Memory Used | Beats 100% in Time and Space
IntuitionThe in-place modification works by overwriting each pair's left position with their sum modulo 10, while the inner loop processes pairs from left to ri
# Intuition
The in-place modification works by overwriting each pair's left position with their sum modulo 10, while the inner loop processes pairs from left to right. Since each step uses the original right digit (not yet overwritten in this iteration), it safely updates positions without affecting subsequent pairs in the same reduction step.
Here is a visualization for the sample input `s = "1234"` using text-based diagrams. Below is a structured representation of the in-place modification steps:

---

### **Sample Input**: `"1 2 3 4"`  
**Goal**: Reduce to 2 digits via iterative in-place updates.

---

### **Iteration 1 (Reduce from 4 → 3 digits)**  
**Original String**:  
```
Indices: 0   1   2   3
Values: 1   2   3   4
```

**Process pairs left-to-right**:
1. **Pair (0,1)**: `(1+2)%10 = 3` → Update index **0**:  
   ```
   Values: 3   2   3   4
   ```
2. **Pair (1,2)**: `(2+3)%10 = 5` → Update index **1**:  
   ```
   Values: 3   5   3   4
   ```
3. **Pair (2,3)**: `(3+4)%10 = 7` → Update index **2**:  
   ```
   Values: 3   5   7   4
   ```

**Result after Iteration 1**: Use first 3 digits → `"3 5 7"`.

---

### **Iteration 2 (Reduce from 3 → 2 digits)**  
**Current String**:  
```
Indices: 0   1   2
Values: 3   5   7
```

**Process pairs left-to-right**:
1. **Pair (0,1)**: `(3+5)%10 = 8` → Update index **0**:  
   ```
   Values: 8   5   7
   ```
2. **Pair (1,2)**: `(5+7)%10 = 2` → Update index **1**:  
   ```
   Values: 8   2   7
   ```

**Final Result**: First 2 digits → `"8 2"` → **Not equal** → `false`.

# Approach
The approach used in the provided code involves simulating each step of the process iteratively until the string is reduced to exactly two digits. Here's a breakdown of the method:

1. **Iterative Reduction**: The code reduces the length of the string by one in each iteration of the outer loop. This is done by processing consecutive pairs of digits and replacing them with their sum modulo 10.
2. **In-Place Modification**: For each iteration, the string is modified in-place. Each pair of consecutive digits is processed from left to right, and the result is stored in the left position of the pair. This ensures that subsequent pairs in the same iteration use the original values of the right digits that haven't been overwritten yet.
3. **Outer Loop Control**: The outer loop runs exactly `n-2` times, where `n` is the initial length of the string. This ensures that after `n-2` reductions, the string is left with exactly two digits.
4. **Final Check**: After all iterations, the first two characters of the modified string are checked for equality to determine the result.

# Complexity
- Time complexity: O(n^2)

- Space complexity: O(1)

# Code
```cpp []
class Solution {
public:
    bool hasSameDigits(string s) {
        int n = s.size();
        for(int j = 0; j<n-2; j++){
            for(int i=1; i<n-j; i++){
                int num = s[i] - '0', prev_num = s[i-1]-'0';
                s[i-1] = ((num + prev_num)%10)+'0';
            }
        }

        return s[0] == s[1];
    }
};
```
['C++']