145A - Lucky Conversion - CodeForces Solution


greedy implementation *1200

Please click on ads to support us..

Python Code:

import random
import sys
import os
import math
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from io import BytesIO, IOBase
from copy import deepcopy
import threading
import bisect

BUFSIZE = 4096


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")


def I():
    return input()


def II():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(input().split())


def LII():
    return list(map(int, input().split()))


def GMI():
    return map(lambda x: int(x) - 1, input().split())


def LGMI():
    return list(map(lambda x: int(x) - 1, input().split()))


def check(limit: int, a: list) -> bool:
    s = 0
    cnt = 0
    for i in range(1, len(a)):
        cnt += 1
        if cnt & 1:
            s += a[i]
        else:
            s -= a[i]
        if cnt == limit:
            cnt = 0
    if s == 0:
        return True
    else:
        return False


def solve() -> None:
    n, k = MI()
    if n == k == 1:
        print(1)
        return ;

    res = 0
    if n & 1:
        if k <= n // 2 + 1:
            res = 1
        else:
            k -= n // 2 + 1
            res = 2
    else:
        if k <= n // 2:
            res = 1
        else:
            k -= n / 2
            res = 2
    if k == 0:
        print(res)
    else:
        for _ in range(k):
            res += 2
        print(res)








if __name__ == '__main__':
    a=I()
    b=I()
    p=q=0
    for i in range(len(a)):
        if a[i]=='7':
            p+=1
        if b[i]=='7':
            q+=1
    res=c=abs(p-q)
    s=""
    if p>q:        ch="4"
    else:ch="7"
    for i in range(len(a)):
        if a[i]!=ch and c>0 and a[i]!=b[i]:
            c-=1
            s+=ch
        else:s+=a[i]
    ans=0
    for i in range(len(b)):
        if b[i]!=s[i]:ans+=1
    print(res+(ans//2))









  		 		 				 		 	  				    		 	

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a , b ;
    cin >> a >> b ;
    int say1=0,say2=0;
    for(int i=0;i<a.size();++i)
    {
        if(a[i]!=b[i])
        {
            if(a[i]=='4'){
                say1++;
            }
            else {
                say2++;
        }
    }
    }
    cout<<max(say1 , say2) ;
}


Comments

Submit
0 Comments
More Questions

145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians