classSolution: defcountAsterisks(self, s: str) -> int: valid = True res = 0 for c in s: if c == '|': valid = not valid elif c == '*'and valid: res += 1 return res
publicclassSolution { publicintCountAsterisks(string s) { bool valid = true; int res = 0; foreach (char c in s) { if (c == '|') { valid = !valid; } elseif (c == '*' && valid) { res++; } } return res; } }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intcountAsterisks(string s){ bool valid = true; int res = 0; for (int i = 0; i < s.size(); i++) { char c = s[i]; if (c == '|') { valid = !valid; } elseif (c == '*' && valid) { res++; } } return res; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13
intcountAsterisks(char * s) { bool valid = true; int res = 0; for (int i = 0; s[i] != '\0'; i++) { char c = s[i]; if (c == '|') { valid = !valid; } elseif (c == '*' && valid) { res++; } } return res; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var countAsterisks = function(s) { let valid = true; let res = 0; for (let i = 0; i < s.length; i++) { let c = s[i]; if (c === '|') { valid = !valid; } elseif (c === '*' && valid) { res++; } } return res; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11
funccountAsterisks(s string) (res int) { valid := true for _, c := range s { if c == '|' { valid = !valid } elseif c == '*' && valid { res++ } } return }