Lexicographically Smallest Palindrome Transformation
IntuitionThe goal is to transform the given string into the lexicographically smallest palindrome by modifying characters as needed. Since a palindrome reads th
---

# **Intuition**  
The goal is to transform the given string into the **lexicographically smallest palindrome** by modifying characters as needed. Since a palindrome reads the same forward and backward, we can compare corresponding characters from both ends and replace the larger one with the smaller one.

---

# **Approach**  
1. Convert the input string into an array (`arr`) so we can modify characters efficiently.  
2. Use two pointers:  
   - `i` starts from the beginning (`0`).  
   - `j` starts from the end (`s.length - 1`).  
3. Iterate while `i <= j`:  
   - If `arr[i] !== arr[j]`, replace both characters with the **smaller** one to ensure lexicographical order.  
   - Move the pointers (`i++` and `j--`) toward the center.  
4. Convert the modified array back into a string and return it.  

---

# **Complexity Analysis**  
- **Time Complexity:** $$O(n)$$, as we iterate through the string once.  
- **Space Complexity:** $$O(n)$$, due to converting the string into an array.  

---

# **Code**
```javascript
/**
 * @param {string} s
 * @return {string}
 */
var makeSmallestPalindrome = function(s) {
    let i = 0, j = s.length - 1;
    let arr = s.split(""); // Convert string to array
    
    while (i <= j) {
        if (arr[i] !== arr[j]) {
            const minChar = arr[i] < arr[j] ? arr[i] : arr[j]; // Choose the smaller character
            arr[i] = arr[j] = minChar; // Make them equal
        }
        i++;
        j--;
    }
    
    return arr.join(""); // Convert array back to string
};

// Example
console.log(makeSmallestPalindrome("egcfe")); // Output: "efcfe"
```

---

# **Example Walkthrough**  
### **Input:** `"egcfe"`  
### **Step-by-step transformation:**  
1. Compare `s[0] = 'e'` and `s[4] = 'e'` → No change.  
2. Compare `s[1] = 'g'` and `s[3] = 'f'` → Replace both with `'f'`, result: `"efcfe"`.  
3. `s[2] = 'c'` is in the middle → No change needed.  
4. Final palindrome: `"efcfe"`.  

---

This approach guarantees the **lexicographically smallest** palindrome transformation while maintaining efficiency. 🚀
['JavaScript']