aboutsummaryrefslogtreecommitdiff
path: root/remove_adjacent_equal_char.c
blob: f3a82315e3c1eb4ab5af9f6b63fd6a74c91b6e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 2957. Remove Adjacent Almost-Equal Characters
#include "leetcode.h"

/*
 * 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' or 'a' and 'b' are adjacent in the alphabet
 */

int removeAlmostEqualCharacters(char *word) {
  int n = strlen(word), ans = 0;
  if (n == 1)
    return 0;
  else if (n == 2)
    return abs(word[0] - word[1]) < 2 ? 1 : 0;
  for (int i = 0; i < n - 2; i++) {
    if (abs(word[i + 1] - word[i]) < 2) {
      if (abs(word[i + 1] - word[i + 2]) < 2) {
        ans++;
        word[i + 1] = ' ';
      } else {
        ans++;
        word[i] = ' ';
      }
    } else if (abs(word[i + 1] - word[i + 2]) < 2) {
      ans++;
      word[i + 2] = ' ';
    }
  }
  return ans;
}

int main() {
  char *w1 = "aaaaa", *w2 = "abddez", *w3 = "zyxyxyz";
  printf("%d\n", removeAlmostEqualCharacters(w1)); // expect: 2
  printf("%d\n", removeAlmostEqualCharacters(w2)); // expect: 2
  printf("%d\n", removeAlmostEqualCharacters(w3)); // expect: 3
}