Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 213f601

Browse files
number of segments in a string
1 parent ab8aefc commit 213f601

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package easy;
2+
/**434. Number of Segments in a String My SubmissionsBack To Contest
3+
4+
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
5+
6+
Please note that the string does not contain any non-printable characters.
7+
8+
Example:
9+
10+
Input: "Hello, my name is John"
11+
Output: 5*/
12+
public class NumberofSegmentsinaString {
13+
14+
public static int countSegments(String s) {
15+
if (s == null || s.isEmpty()) return 0;
16+
String[] segments = s.split(" ");
17+
int count = 0;
18+
for (String seg : segments){
19+
if (seg.equals("")) continue;
20+
count++;
21+
}
22+
return count;
23+
}
24+
25+
public static void main(String...args){
26+
String test = ", , , , a, eaefa";
27+
System.out.println(countSegments(test));
28+
}
29+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)|[Solution](../../blob/master/EASY/src/easy/FindAllAnagramsinaString.java)| O(n)|O(1) | Easy|
1414
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../../blob/master/MEDIUM/src/medium/FindRightInterval.java) | O(nlogn) |O(n) | Medium| Binary Search
1515
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../../blob/master/MEDIUM/src/medium/NonOverlappingIntervals.java) | O(nlogn) |O(1) | Medium| Greedy
16+
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[Solution](../../blob/master/EASY/src/easy/NumberofSegmentsinaString.java)| O(n)|O(1) | Easy|
1617
|420|[Strong Password Checker](https://leetcode.com/problems/strong-password-checker/)|[Solution](../../blob/master/HARD/src/hard/StrongPasswordChecker.java)| ? | ? | Hard|
1718
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../../blob/master/MEDIUM/src/medium/BattleshipsinaBoard.java) | O(n^2) |O(1) | Medium| DFS
1819
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|[Solution](../../blob/master/MEDIUM/src/medium/PacificAtlanticWaterFlow.java) | O(m*n*Max(m,n)) |O(m*n) | Medium| DFS

0 commit comments

Comments
 (0)