From ae7c434857555c74c4804d1cf9adc6d447e20e88 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 30 Jun 2025 20:32:54 -0600 Subject: 3330. find the original typed string 1 --- find_original_typed_str1.c | 26 ++++++++++++++++++++++++++ find_original_typed_str1.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 find_original_typed_str1.c create mode 100644 find_original_typed_str1.py diff --git a/find_original_typed_str1.c b/find_original_typed_str1.c new file mode 100644 index 0000000..ce60027 --- /dev/null +++ b/find_original_typed_str1.c @@ -0,0 +1,26 @@ +// 3330. Find the Original Typed String I +#include "leetcode.h" + +/* + * alice is attmpting to type a specific string on her computer. however, she + * tends to be clumsy and may press a key for too long, resulting a character + * being typed multiple times. although alice tried to focus on her typing, she + * is aware that she may still have done this at least once. you are given a + * string 'word', which represents the final output displayed on alices screen. + * return the total number of possible original strings that alice might have + * intended to type. + */ + +int possibleStringCount(char *word) { + int n = strlen(word), ans = n; + for (int i = 1; i < n; ++i) + ans -= word[i] != word[i - 1]; + return ans; +} + +int main() { + char *w1 = "abbcccc", *w2 = "abcd", *w3 = "aaaa"; + printf("%d\n", possibleStringCount(w1)); // expect: 5 + printf("%d\n", possibleStringCount(w2)); // expect: 1 + printf("%d\n", possibleStringCount(w3)); // expect: 4 +} diff --git a/find_original_typed_str1.py b/find_original_typed_str1.py new file mode 100644 index 0000000..d4d59b0 --- /dev/null +++ b/find_original_typed_str1.py @@ -0,0 +1,28 @@ +# 3330. Find the Original Typed String I +from itertools import groupby + +""" +alice is attmpting to type a specific string on her computer. however, she +tends to be clumsy and may press a key for too long, resulting a character +being typed multiple times. although alice tried to focus on her typing, she +is aware that she may still have done this at least once. you are given a +string 'word', which represents the final output displayed on alices screen. +return the total number of possible original strings that alice might have +intended to type. +""" + + +class Solution(object): + def possibleStringCount(self, word): + """ + :type word: str + :rtype: int + """ + return len(word) - len(list(groupby(word))) + 1 + + +if __name__ == "__main__": + obj = Solution() + print(obj.possibleStringCount(word="abbcccc")) + print(obj.possibleStringCount(word="abcd")) + print(obj.possibleStringCount(word="aaaa")) -- cgit v1.2.3