Skip to content

Commit bce744b

Browse files
committed
Add solution
1 parent 9e8dc5a commit bce744b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// 3330. Find the Original Typed String I
2+
// Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and may press a key for too long, resulting in a character being typed multiple times.
3+
// Although Alice tried to focus on her typing, she is aware that she may still have done this at most once.
4+
// You are given a string word, which represents the final output displayed on Alice's screen.
5+
// Return the total number of possible original strings that Alice might have intended to type.
6+
7+
8+
// Solution: Counting
9+
10+
// Find the groups of consecutively equal characters with more than one occurrance.
11+
// For each group, the possible strings is equal to the occurrances in the group - 1.
12+
// Get the sum of occurrances-1.
13+
14+
// Time Complexity: O(n) 45ms
15+
// Space Complexity: O(1) 54MB
16+
function possibleStringCount(word) {
17+
let consec = 1, count = 0;
18+
const n = word.length;
19+
for (let i = 1; i < n; i++) {
20+
if (word[i] === word[i - 1]) {
21+
consec++;
22+
} else {
23+
count += consec > 1 ? consec - 1 : 0;
24+
consec = 1;
25+
}
26+
}
27+
if (consec > 1) {
28+
count += consec - 1;
29+
}
30+
return count + 1; // the original string itself
31+
};
32+
33+
// Three test cases
34+
console.log(possibleStringCount("abbcccc")) // 5
35+
console.log(possibleStringCount("abcd")) // 1
36+
console.log(possibleStringCount("aaaa")) // 4

0 commit comments

Comments
 (0)