지우너

[코드트리] G&H 반전시키기 C++ 본문

Problem Solving

[코드트리] G&H 반전시키기 C++

지옹 2024. 9. 10. 20:40

문제

https://www.codetree.ai/missions/8/problems/reversing-g-and-h?&utm_source=clipboard&utm_medium=text

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

 

코드

#include <iostream>
using namespace std;

int n;
string initStr, targetStr;

int main() {
    cin >> n >> initStr >> targetStr;

    // 목표 str과 다른 문자 그룹의 수(answer)
    int checkIdx=0, answer=0;
    while(checkIdx<n){
        // 다르면 
        if(initStr[checkIdx]!=targetStr[checkIdx]){
            while(checkIdx+1<n && initStr[checkIdx]!=targetStr[checkIdx]){
                checkIdx++;
            }
            answer++;
        }
        checkIdx++;
    }
    cout << answer << '\n';
    return 0;
}