Remove Adjacent Almost-Equal Characters - Problem

You are given a 0-indexed string word. In one operation, you can pick any index i of word and change word[i] to any lowercase English letter.

Return the minimum number of operations needed to remove all adjacent almost-equal characters from word.

Two characters a and b are almost-equal if:

  • a == b (same character), or
  • a and b are adjacent in the alphabet (like 'a' and 'b', or 'p' and 'q')

Input & Output

Example 1 — Basic Conflict
$ Input: word = "abb"
Output: 1
💡 Note: Characters at positions 1 and 2 are both 'b' (almost-equal). Change word[2] to any character that's not adjacent to 'b', like 'd'. Result: "abd" with 1 operation.
Example 2 — Adjacent Letters
$ Input: word = "abc"
Output: 1
💡 Note: Characters 'b' and 'c' are adjacent in alphabet (almost-equal). Change 'c' to a non-adjacent character like 'e'. Result: "abe" with 1 operation.
Example 3 — No Changes Needed
$ Input: word = "ace"
Output: 0
💡 Note: No adjacent characters are almost-equal: 'a' and 'c' differ by 2, 'c' and 'e' differ by 2. No operations needed.

Constraints

  • 1 ≤ word.length ≤ 105
  • word consists only of lowercase English letters

Visualization

Tap to expand
Remove Adjacent Almost-Equal CharactersInput: "abb"abb← Conflict!Change b→dOutput: "abd"abd✓ Safe!Operations: 1
Understanding the Visualization
1
Input Analysis
Identify almost-equal adjacent characters
2
Greedy Fix
Change characters to eliminate conflicts
3
Final Result
Count total operations needed
Key Takeaway
🎯 Key Insight: Always change the right character in conflicts to minimize total operations
Asked in
Google 25 Meta 18 Amazon 15
23.4K Views
Medium Frequency
~15 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen