LeetCode 3330: Find the Original Typed String I

String

Problem Description

Explanation

To solve this problem, we can iterate through the input string word and count the number of occurrences of each character. For each character, we calculate the number of ways it can be typed (taking into account that it might have been typed multiple times). The total number of possible original strings is the product of the number of ways each character can be typed. We need to consider the cases where a character is typed once, twice, or more than twice.

Solutions

class Solution {
    public int findOriginalString(String word) {
        int result = 1;
        int count = 0;
        for (int i = 0; i < word.length(); i++) {
            count++;
            if (i == word.length() - 1 || word.charAt(i) != word.charAt(i + 1)) {
                result *= count;
                count = 0;
            }
        }
        return result;
    }
}

Loading editor...