TheAlgorithms-C/leetcode/src/2222.c
Alexander Pantyukhin 0169283f55
feat: add Number of Ways to Select Buildings (#1140)
* 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>
2022-12-18 21:34:24 -06:00

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');
}