Skip to content

Commit 163ac60

Browse files
easy string logic count
Q1. Find the Original Typed String I Solved Easy 3 pt. 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. Although Alice tried to focus on her typing, she is aware that she may still have done this at most once. You are given a string word, which represents the final output displayed on Alice's screen. Return the total number of possible original strings that Alice might have intended to type. Example 1: Input: word = "abbcccc" Output: 5 Explanation: The possible strings are: "abbcccc", "abbccc", "abbcc", "abbc", and "abcccc". Example 2: Input: word = "abcd" Output: 1 Explanation: The only possible string is "abcd". Example 3: Input: word = "aaaa" Output: 4 Constraints: 1 <= word.length <= 100 word consists only of lowercase English letters.
1 parent a3c2c0b commit 163ac60

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int possibleStringCount(String word) {
3+
int ans=1;
4+
int n=word.length();
5+
int i=0;
6+
while(i<n){
7+
int count=1;
8+
while(i+count<n && word.charAt(i)==word.charAt(i+count)){
9+
count++;
10+
}
11+
ans=ans+count-1;
12+
i=i+count;
13+
}
14+
return ans;
15+
}
16+
}

0 commit comments

Comments
 (0)