题目:

You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa.

The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not.

Return the minimum number of operations needed to make s alternating.

Example 1:

Input: s = "0100"
Output: 1
Explanation: If you change the last character to '1', s will be "0101", which is alternating.

Example 2:

Input: s = "10"
Output: 0
Explanation: s is already alternating.

Example 3:

Input: s = "1111"
Output: 2
Explanation: You need two operations to reach "0101" or "1010".

Constraints:

  • 1 <= s.length <= 10^4
  • s[i] is either '0' or '1'.

思路:

按照题意,alternating字符串无非是0101或者1010,那么我们就创造一下这两种字符串,和原字符串等长,然后与原字符串对比得出不同的位置数再返回最小值即可。同时这里可以边创建字符串边和原字符串比较,因此two pass即可。

代码:

class Solution {
public:
    int minOperations(string s) {
        int n=s.size();        
        int c1=0, c2=0;
        string s1="";
        for(int i=0;i<n;i++)
        {
            if(i%2==0)
                s1+='0';
            else
                s1+='1';
            if(s1[i]!=s[i])
                c1++;
        }
        string s2="";
        for(int i=0;i<n;i++)
        {
            if(i%2==0)
                s2+='1';
            else
                s2+='0';
            if(s2[i]!=s[i])
                c2++;
        }    
        return min(c1,c2);
    }
};

1758. Minimum Changes To Make Alternating Binary String相关推荐

  1. Binary String Minimizing CodeForces - 1256D(贪心)

    You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1'). I ...

  2. 题解 CF1399D 【Binary String To Subsequences】

    题目链接:http://codeforces.com/contest/1399/problem/D 题目描述: You are given a binary string s consisting o ...

  3. Binary String Reconstruction CodeForces - 1352F(思维+构造)

    For some binary string s (i.e. each character si is either '0' or '1'), all pairs of consecutive (ad ...

  4. 【2019牛客暑期多校训练营(第三场)- B】Crazy Binary String(思维,01串,前缀和)

    题干: 链接:https://ac.nowcoder.com/acm/contest/883/B 来源:牛客网 ZYB loves binary strings (strings that only ...

  5. nyoj 题目5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alpha ...

  6. Binary String Matching

    Binary String Matching 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your t ...

  7. Binary String Constructing(CF-1003B)

    Problem Description You are given three integers aa, bb and xx. Your task is to construct a binary s ...

  8. 2019牛客暑期多校训练营(第三场) B-Crazy Binary String

    题目描述 ZYB喜欢二进制字符串(只包含'0'和'1'的字符串).他喜欢equal binary string(其中字符串中的"0"数和"1"数相等) ZYB想 ...

  9. D1. Kirk and a Binary String (easy version)

    题目链接:http://codeforces.com/contest/1204/problem/D1 D1. Kirk and a Binary String (easy version) time ...

最新文章

  1. python基础——继承与派生、组合
  2. HPU组队赛B:问题(二进制枚举)
  3. Android OpenGL使用GLSurfaceView预览视频
  4. [Qt教程] 第46篇 进阶(六) 国际化
  5. java byte 拓展_Java项目中如何扩展第三方jar包中的类?
  6. highcharts第一篇---简介和使用
  7. FileStream功能被禁用
  8. CAD入门系列[完]
  9. 网页常用字体 【参考】
  10. “只用 1 分钟” - 超简极速 Apk 签名 多渠道打包神器
  11. [米家]窗帘电机【立创开源】
  12. 潇洒郎:VMware固定虚拟机IP地址
  13. 联想笔记本怎么找计算机放桌面,联想笔记本电脑便签在哪,笔记本电脑便签在哪里?...
  14. 【问题解决】Springboot中@Value()读取不到配置文件属性解决方法
  15. windows下安装mingw-w64
  16. 简述Java中封装,继承,多态的理解
  17. 精确率(precision)、召回率(recall)、准确率(accuracy)
  18. systemctl enable与systemctl start的区别
  19. torchAudio中wav2vec2的源码(三)——transformer-encoder的构建
  20. (14)Delphi下利用JVCL组件包快速实现——解释Pascal语言的脚本引擎

热门文章

  1. python leetcode 202. Happy Number
  2. HTML5CSS3笔记:响应式设计中的 HTML5
  3. JPEX联手西悉尼流浪者队 推出250款独家NFT“J-ball”
  4. 如何在lion系统下安装Xcode 3.2.x版本
  5. 计算机专业吐槽搞笑,大学里那些专业搞笑描述,简直太形象了!网友:逗死我了!...
  6. 爬取段子网里面的搞笑段子
  7. 视频教程:Java七大外企经典面试套路之基础篇
  8. 期货开户需要具备⼀定的条件
  9. 操作系统-移动操作系统-百科: iOS(苹果公司的移动操作系统)
  10. 分享让世界更美丽,TopSemic有奖征文