2264. Largest 3-Same-Digit Number in String ¶ Time: $O(n)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12class Solution { public: string largestGoodInteger(string num) { string ans; for (int i = 2; i < num.length(); ++i) if (num[i - 2] == num[i - 1] && num[i - 1] == num[i]) ans = max(ans, num.substr(i - 2, 3)); return ans; } }; 1 2 3 4 5 6 7 8 9 10 11 12class Solution { public String largestGoodInteger(String num) { String ans = ""; for (int i = 2; i < num.length(); ++i) if (num.charAt(i - 2) == num.charAt(i - 1) && num.charAt(i - 1) == num.charAt(i) && num.substring(i - 2, i + 1).compareTo(ans) > 0) ans = num.substring(i - 2, i + 1); return ans; } } 1 2 3 4 5class Solution: def largestGoodInteger(self, num: str) -> str: return max(num[i - 2:i + 1] if num[i] == num[i - 1] == num[i - 2] else '' for i in range(2, len(num)))