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 <PalAditya@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Alexander Pantyukhin <apantykhin@gmail.com>
This commit is contained in:
Aditya Pal 2023-04-28 01:07:59 +05:30 committed by GitHub
parent d07ab7d0f1
commit 2b885108b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

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

20
leetcode/src/434.c Normal file
View File

@ -0,0 +1,20 @@
// Given a string s, returns the number of segments in the string.
int countSegments(char * s){
int sLen = strlen(s);
int prevSpace = 1;
int result = 0;
char currChar;
for (int i = 0; i < sLen; i++){
currChar = s[i];
//A string of whitespaces will only be counted once as the condition below is only true when we transition from whitespace to non-whitespace.
//Since we start with assumed whitespace (prevSpace = 1), initial whitespaces are handled as well, if any
if (s[i] != ' ' && prevSpace) {
result++;
}
prevSpace = (currChar == ' ');
}
return result;
}