Skip to content

Commit 2b88510

Browse files
PalAdityagithub-actions[bot]alexpantyukhin
authored
feat: add LeetCode problem 434 (#1252)
* feat: add LeetCode problem 434 Adds solution of problem 434 for leetcode. Beats 100% Time, 97.18% space * docs: updating `leetcode/DIRECTORY.md` * Update 434.c --------- Co-authored-by: PalAditya <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Alexander Pantyukhin <[email protected]>
1 parent d07ab7d commit 2b88510

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

leetcode/DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string) | [C](./src/387.c) | Easy |
9090
| 389 | [Find the Difference](https://leetcode.com/problems/find-the-difference) | [C](./src/389.c) | Easy |
9191
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves) | [C](./src/404.c) | Easy |
92+
| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string) | [C](./src/434.c) | Easy |
9293
| 442 | [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array) | [C](./src/442.c) | Medium |
9394
| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance) | [C](./src/461.c) | Easy |
9495
| 476 | [Number Complement](https://leetcode.com/problems/number-complement) | [C](./src/476.c) | Easy |

leetcode/src/434.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Given a string s, returns the number of segments in the string.
2+
int countSegments(char * s){
3+
int sLen = strlen(s);
4+
int prevSpace = 1;
5+
int result = 0;
6+
char currChar;
7+
8+
for (int i = 0; i < sLen; i++){
9+
currChar = s[i];
10+
11+
//A string of whitespaces will only be counted once as the condition below is only true when we transition from whitespace to non-whitespace.
12+
//Since we start with assumed whitespace (prevSpace = 1), initial whitespaces are handled as well, if any
13+
if (s[i] != ' ' && prevSpace) {
14+
result++;
15+
}
16+
prevSpace = (currChar == ' ');
17+
}
18+
19+
return result;
20+
}

0 commit comments

Comments
 (0)