Skip to content

← leetcode

Find the Original Typed String I

· Easy

LeetCode problem 3330

Just iterate through the array and check the previous letter; if it is the same, then you can just append one to the ans counter (which is preset to 1 initially).

code

class Solution {
public:
    int possibleStringCount(string word){
        int ans = 1;
        char c = word[0];
        for (int i = 1; i < word.size(); ++i)
        {
            if (c != word[i]) {
                c = word[i];
            } else {
                ++ans;
            }
        }
        return ans;
    }
};

complexity

  • Time: O(n) where n is the size of word
  • Space: O(1)