Skip to main content
Back to problems
#2330
Medium Algorithms

Valid palindrome iv

Two Pointers String
75.6% acceptance
Mar 31, 2026
111
35
You are given a 0-indexed string s consisting of only lowercase English letters. In one operation, you can change any character of s to any other character. Return true if you can make s a palindrome after performing exactly one or two operations, or return false otherwise.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn make_palindrome(s: String) -> bool {
    let bytes = s.as_bytes();
    let n = bytes.len();
    let mut mismatches = 0;
    let mut l = 0;
    let mut r = n - 1;
    while l < r {
      if bytes[l] != bytes[r] {
        mismatches += 1;
        if mismatches > 2 {
          return false;
        }
      }
      l += 1;
      r -= 1;
    }
    true
  }
}