aboutsummaryrefslogtreecommitdiff
path: root/largest_3_digit_string.c
blob: baba195a964d26709d5211c9ef489a35a9e48449 (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
// 2264. Largest 3-Same-Digit Number in String
#include "leetcode.h"
#include <stdlib.h>
#include <string.h>

/*
 * given a string 'num' representing a large integer, an integer is good if it
 * meets the following conditions: if it is a substring of 'num' with length
 * '3', it consists of only one unique digit. return the maximum good integer as
 * a string or an empty string "" if no such integer.
 */

char *largestGoodInteger(char *num) {
  static char *patterns[] = {"999", "888", "777", "666", "555",
                             "444", "333", "222", "111", "000"};
  for (int i = 0; i < 10; i++)
    if (strstr(num, patterns[i]))
      return patterns[i];
  return "";
}

int main() {
  char *n1 = "6777133339", *lgi1 = largestGoodInteger(n1);
  char *n2 = "2300019", *lgi2 = largestGoodInteger(n2);
  char *n3 = "42352338", *lgi3 = largestGoodInteger(n3);
  printf("%s\n", lgi1); // expect: 777
  printf("%s\n", lgi2); // expect: 000
  printf("%s\n", lgi3); // expect: NULL
}