mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
0169283f55
* add Number of Ways to Select Buildings * Update 2222.c add new line at the end * Rename README.md to DIRECTORY.md * Update leetcode/src/2222.c Co-authored-by: Taj <tjgurwara99@users.noreply.github.com> * fix review notes Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
31 lines
875 B
C
31 lines
875 B
C
long numberOfWaysForChar(char * s, char c){
|
|
long firstBuildingAppearNumber = 0;
|
|
long secondBuildingAppearNumber = 0;
|
|
long result = 0;
|
|
|
|
int sLength = strlen(s);
|
|
for (int i = 0; i < sLength; i++){
|
|
if (s[i] == c){
|
|
result += secondBuildingAppearNumber;
|
|
|
|
firstBuildingAppearNumber += 1;
|
|
continue;
|
|
}
|
|
|
|
secondBuildingAppearNumber += firstBuildingAppearNumber;
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// numberOfWays returns the sum of number ways of selecting first building
|
|
// and the number of ways of selecting second building which gives us the
|
|
// number of ways of selecting three building such that no
|
|
// consecutive buildings are in the same category.
|
|
// Runtime: O(n)
|
|
// Space: O(n)
|
|
long long numberOfWays(char * s){
|
|
return numberOfWaysForChar(s, '0') + numberOfWaysForChar(s, '1');
|
|
}
|