Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/java/com/leetcode/easy/CountAsterisks2315.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

public class CountAsterisks2315 {
/**
* Let n = s.length() and m = words.length (the number of split segments), where m <= n + 1.
* Time complexity: O(n)
* Breakdown:
* - Split operation: s.split("\\|") iterates through the entire string - O(n)
* - Outer loop: Iterates through half of the segments - O(n/2)
* - Inner loop: Total characters checked is at most n - O(n)
* - Outer loop: Iterates through about half of the segments - O(m/2) = O(m)
* - Inner loop: Across all iterations, the total number of characters checked is at most n - O(n)
* Overall: O(n)
* <p>
* Space complexity: O(n)
* Breakdown:
* - Array storage: String[] words stores segments - O(n)
* - Array storage: String[] words holds m references to substrings whose total length is O(n) - O(n)
*/
public int countAsterisks(String s) {
String[] words = s.split("\\|");
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/leetcode/easy/CountAsterisks2315Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void setUp() {
}

@Test
void testTwoSum_Example1() {
void testCountAsterisks_Example1() {
String s = "l|*e*et|c**o|*de|";

int result = solution.countAsterisks(s);
Expand All @@ -23,7 +23,7 @@ void testTwoSum_Example1() {
}

@Test
void testTwoSum_Example2() {
void testCountAsterisks_Example2() {
String s = "iamprogrammer";

int result = solution.countAsterisks(s);
Expand All @@ -32,7 +32,7 @@ void testTwoSum_Example2() {
}

@Test
void testTwoSum_Example3() {
void testCountAsterisks_Example3() {
String s = "yo|uar|e**|b|e***au|tiful";

int result = solution.countAsterisks(s);
Expand Down