classSolution: defmaxPower(self, s: str) -> int: ans, cnt = 1, 1 for i inrange(1, len(s)): if s[i] == s[i - 1]: cnt += 1 ans = max(ans, cnt) else: cnt = 1 return ans
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: intmaxPower(string s){ int ans = 1, cnt = 1; for (int i = 1; i < s.length(); ++i) { if (s[i] == s[i - 1]) { ++cnt; ans = max(ans, cnt); } else { cnt = 1; } } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { publicintmaxPower(String s) { intans=1, cnt = 1; for (inti=1; i < s.length(); ++i) { if (s.charAt(i) == s.charAt(i - 1)) { ++cnt; ans = Math.max(ans, cnt); } else { cnt = 1; } } return ans; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassSolution { publicintMaxPower(string s) { int ans = 1, cnt = 1; for (int i = 1; i < s.Length; ++i) { if (s[i] == s[i - 1]) { ++cnt; ans = Math.Max(ans, cnt); } else { cnt = 1; } } return ans; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcmaxPower(s string)int { ans, cnt := 1, 1 for i := 1; i < len(s); i++ { if s[i] == s[i-1] { cnt++ if cnt > ans { ans = cnt } } else { cnt = 1 } } return ans }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var maxPower = function(s) { let ans = 1, cnt = 1; for (let i = 1; i < s.length; ++i) { if (s[i] == s[i - 1]) { ++cnt; ans = Math.max(ans, cnt); } else { cnt = 1; } } return ans; };