File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed
Bonus29 - Find the Original Typed String I Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ LeetCode 3330 – Find the Original Typed String I
2
+
3
+ class Solution {
4
+ public int possibleStringCount(String word) {
5
+ int ans = 1;
6
+ for (int i = 1; i < word.length(); ++i)
7
+ if (word.charAt(i) == word.charAt(i - 1))
8
+ ++ans;
9
+ return ans;
10
+ }
11
+ }
12
+
13
+ Explanation
14
+
15
+ int ans = 1;
16
+ 🔹 Minimum ek character toh original hoga hi (starting character).
17
+
18
+ for (int i = 1; i < word.length(); ++i)
19
+ 🔹 Loop chala rahe hain second character se end tak.
20
+
21
+ if (word.charAt(i) == word.charAt(i - 1)) ++ans;
22
+ 🔹 Agar current character same hai previous ke, toh wo repeat type hai
23
+ 🔹 Toh valid typed key hai → ans++
24
+
25
+ return ans;
26
+ 🔹 Total valid typed characters count return kar do.
27
+
28
+ 🧪 Example
29
+ Input: "aabbcc"
30
+ Explanation:
31
+
32
+ a = original
33
+
34
+ a = repeat → +1
35
+
36
+ b = new
37
+
38
+ b = repeat → +1
39
+
40
+ c = new
41
+
42
+ c = repeat → +1
43
+
44
+ Output: 6
45
+
46
+ Time & Space Complexity
47
+ Metric Value
48
+ Time O(n)
49
+ Space O(1)
50
+
51
+ Need more help or Java tricks?
52
+ https://www.linkedin.com/in/saurabh884095/
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int possibleStringCount (String word ) {
3
+ int ans = 1 ;
4
+ for (int i = 1 ; i < word .length (); ++i )
5
+ if (word .charAt (i ) == word .charAt (i - 1 ))
6
+ ++ans ;
7
+ return ans ;
8
+ }
9
+ }
You can’t perform that action at this time.
0 commit comments