Morgan and a String

Sort by

recency

|

310 Discussions

|

  • + 0 comments

    Do you have any idea why i'm getting just half of the test cases? def morganAndString(a, b):

    # Write your code here
    morgan_str=''
    while len(a) > 0 and len(b) > 0:
        if a <= b:
            morgan_str+=a[0]
            a= a[1:]
        else:
            morgan_str+=b[0]
            b= b[1:]
    
    if len(a) == 0:
        morgan_str+=b
    else:
        morgan_str+=a
    return morgan_str
    
  • + 0 comments

    Could any one give a simple case that might fail this logic? def morganAndString(a, b): j=0 k=0 c=[] while jb[k:]: c+=b[k] k+=1 if j

  • + 1 comment

    Use this C++ Code

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool compare(const string &a, int i, const string &b, int j) {
        while (i < static_cast<int>(a.length()) && j < static_cast<int>(b.length())) {
            if (a[i] < b[j]) return true;
            else if (a[i] > b[j]) return false;
            i++;
            j++;
        }
        return i == static_cast<int>(a.length()) ? false : true;
    }
    
    string morganAndString(const string &a, const string &b) {
        int lenA = a.length(), lenB = b.length();
        string result;
        int pA = 0, pB = 0;
    
        while (pA < lenA && pB < lenB) {
            if (a[pA] < b[pB]) {
                result += a[pA++];
            } else if (a[pA] > b[pB]) {
                result += b[pB++];
            } else {
                if (compare(a, pA + 1, b, pB + 1)) {
                    result += a[pA++];
                    while (pA < lenA && a[pA] == a[pA - 1]) {
                        result += a[pA++];
                    }
                } else {
                    result += b[pB++];
                    while (pB < lenB && b[pB] == b[pB - 1]) {
                        result += b[pB++];
                    }
                }
            }
        }
    
        if (pA < lenA) {
            result += a.substr(pA);
        }
    
        if (pB < lenB) {
            result += b.substr(pB);
        }
    
        return result;
    }
    
    int main() {
        int t;
        cin >> t;
        for (int a0 = 0; a0 < t; a0++) {
            string a, b;
            cin >> a >> b;
            string result = morganAndString(a, b);
            cout << result << endl;
        }
        return 0;
    }
    
  • + 0 comments
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
        static String morganAndString(String a, String b) {
            // Complete this function
            int lenA = a.length(), lenB = b.length();
            StringBuilder sb = new StringBuilder();
            int pA = 0, pB = 0;
            while (pA < lenA && pB < lenB) {
                if (a.charAt(pA) < b.charAt(pB)) {
                    sb.append(a.charAt(pA++));
                }else if (a.charAt(pA) > b.charAt(pB)) {
                    sb.append(b.charAt(pB++));
                }else {
                    if (compare(a, pA + 1, b, pB + 1)) {
                        sb.append(a.charAt(pA++));
                        while (pA < a.length() && a.charAt(pA) == a.charAt(pA - 1)) {
                            sb.append(a.charAt(pA++));
                        }
                    } else {
                        sb.append(b.charAt(pB++));
                        while (pB < b.length() && b.charAt(pB) == b.charAt(pB - 1)) {
                            sb.append(b.charAt(pB++));
                        }
                    }
                }
            }
            
            if (pA < lenA) {
                sb.append(a.substring(pA));
            }
            
            if (pB < lenB) {
                sb.append(b.substring(pB));
            }
            
            return sb.toString();
        }
        
        private static boolean compare(String a, int i, String b, int j) {
            while (i < a.length() && j < b.length()) {
                if (a.charAt(i) < b.charAt(j)) return true;
                else if (a.charAt(i) > b.charAt(j)) return false;
                i++;
                j++;
            }
            
            return i == a.length() ? false : true;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int t = in.nextInt();
            for(int a0 = 0; a0 < t; a0++){
                String a = in.next();
                String b = in.next();
                String result = morganAndString(a, b);
                System.out.println(result);
            }
            in.close();
        }
    }
    

    so use this for good solution

  • + 0 comments
    def morganAndString():
        for test in range(int(input())):
            a, b = (input() + 'z' for line in range(2))
            i = j = 0
            result = ''
            while not a[i] == b[j] == 'z':
                if a[i:] < b[j:]:
                    result += a[i]
                    i += 1
                else:
                    result += b[j]
                    j += 1
            print(result)
    
    if __name__ == '__main__':
        morganAndString()