class Solution {
public int minOperations(String s) {
char[] arr = s.toCharArray();
return Math.min(countOperations(arr, '0'), countOperations(arr,'1'));
}
private int countOperations(char[] arr, char start) {
int cnt = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0 && arr[i] != start) {
cnt++;
}
if (i % 2 == 1 && arr[i] == start) {
cnt++;
}
}
return cnt;
}
}
class Solution:
def minOperations(self, s: str) -> int:
res , difStr0 , difStr1 = 0 , 0 , 0
for i in range(len(s)):
if s[i] == '0':
difStr0 += 0 if i % 2 == 0 else 1
difStr1 += 0 if i % 2 == 1 else 1
else:
difStr0 += 0 if i % 2 == 1 else 1
difStr1 += 0 if i % 2 == 0 else 1
res = min(difStr0 , difStr1)
return res
class Solution {
public boolean checkOnesSegment(String s) {
int cnt = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) != s.charAt(i + 1)) {
cnt++;
if (cnt > 1) return false;
}
}
return true;
}
}
class Solution:
def checkOnesSegment(self, s: str) -> bool:
segments = 0
count = 0
for char in s:
if char == "1":
count += 1
else:
if count > 0:
segments += 1
count = 0
if count > 0:
segments += 1
return segments == 1
class Solution:
def checkOnesSegment(self, s: str) -> bool:
segments = 1
for i in range(1 ,len(s)):
if s[i - 1] == '0' and s[i] == '1': segments += 1
return segments <= 1
public class Solution {
public bool CheckOnesSegment(string s) {
int n = s.Length;
bool flag=false;
for(int i=n-1;i>0;i--)
if(s[i]=='0')
{
if(flag)
return false;
}
else
flag=true;
return true;
}
}
1 thì cũng là chuỗi 1 màXem tệp đính kèm 3502900C++:class Solution { public: bool checkOnesSegment(string s) { return s.find("01") == string::npos; } };
viết description như cc![]()
function checkOnesSegment(s: string): boolean {
for (let i = 1; i < s.length; i++) {
if (s[i] === '1' && s[i - 1] === '0') return false;
}
return true;
};
bỏ có mấy tháng mà giờ code thế này đây, xử bắn gấpPython:class Solution: def checkOnesSegment(self, s: str) -> bool: segments = 0 count = 0 for char in s: if char == "1": count += 1 else: if count > 0: segments += 1 count = 0 if count > 0: segments += 1 return segments == 1
Giờ code như làm văn, cứ nghĩ gì code nấy thôi huynhbỏ có mấy tháng mà giờ code thế này đây, xử bắn gấp![]()

class Solution {
public int minFlips(String s) {
int n = s.length(), res = Integer.MAX_VALUE, diff1 = 0, diff2 = 0;
String t = s + s;
for (int i = 0; i < n * 2; i++) {
char expected1 = (i % 2 == 0) ? '0' : '1';
if (t.charAt(i) != expected1) diff1++;
else diff2++;
if (i >= n) {
char prevExpected1 = ((i - n) % 2 == 0) ? '0' : '1';
if (t.charAt(i - n) != prevExpected1) diff1--;
else diff2--;
}
if (i >= n - 1) res = Math.min(res, Math.min(diff1, diff2));
}
return res;
}
}
huyết mạch áp chếsao hôm nay ít người làm thế