-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
d07ab7d
commit 2b88510
Showing
2 changed files
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |