diff options
author | Jack Sangdahl <[email protected]> | 2025-08-14 11:09:35 +0200 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-08-14 11:09:35 +0200 |
commit | 3ae87fe80a3a60df750acefceff38c4ab09a9a30 (patch) | |
tree | c8c452fb1f0cda546828df1b19e7d600d7db6d6c | |
parent | 129bb98a6f92b0e527d336cc8760efaaef6487d9 (diff) | |
download | leetcode-3ae87fe80a3a60df750acefceff38c4ab09a9a30.tar.gz leetcode-3ae87fe80a3a60df750acefceff38c4ab09a9a30.zip |
2264. largest 3-same digit number in string
-rw-r--r-- | largest_3_digit_string.c | 45 | ||||
-rw-r--r-- | largest_3_digit_string.py | 29 |
2 files changed, 31 insertions, 43 deletions
diff --git a/largest_3_digit_string.c b/largest_3_digit_string.c index 9c9ed41..baba195 100644 --- a/largest_3_digit_string.c +++ b/largest_3_digit_string.c @@ -1,5 +1,7 @@ // 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 @@ -8,37 +10,20 @@ * a string or an empty string "" if no such integer. */ -char *largestGoodInterger(char *num) { - int n = strlen(num), chr = 'a', cnt = 1; - for (int i = 1; i < n; i++) { - if (num[i] == num[i - 1]) { - cnt++; - if (i < n - 1) - continue; - } - if (cnt >= 3) { - if (chr == 'a') - chr = num[i - 1]; - else - chr = fmax(chr, num[i - 1]); - } - cnt = 1; - } - if (chr == 'a') - return ""; - else { - char *ans = malloc(4); - ans[3] = '\0'; - ans[0] = chr; - ans[1] = chr; - ans[2] = chr; - return ans; - } +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", *n2 = "2300019", *n3 = "42352338"; - printf("%s\n", largestGoodInterger(n1)); // expect: 777 - printf("%s\n", largestGoodInterger(n2)); // expect: 000 - printf("%s\n", largestGoodInterger(n3)); // expect: NULL + 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 } diff --git a/largest_3_digit_string.py b/largest_3_digit_string.py index f8789a4..c27c126 100644 --- a/largest_3_digit_string.py +++ b/largest_3_digit_string.py @@ -14,19 +14,22 @@ class Solution(object): :type num: str :rtype: str """ - ans = "" - empty = "" - tmp = 0 - check = True - for i in range(len(num) - 2): - if i + 2 < len(num) and num[i] == num[i + 1] == num[i + 2]: - tmp = max(tmp, int(num[i])) - check = False - if not check: - ans += str(tmp) * 3 - else: - return empty - return ans + patterns = [ + "999", + "888", + "777", + "666", + "555", + "444", + "333", + "222", + "111", + "000", + ] + for i in patterns: + if i in num: + return i + return "" if __name__ == "__main__": |