Skip to content

Commit c2a4c76

Browse files
Solution Leetcode and Explaination Every single line
1 parent c5e3050 commit c2a4c76

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

0 commit comments

Comments
 (0)