Morgan and a String

Sort by

recency

|

311 Discussions

|

  • + 0 comments

    solution in scala

    def morganAndString(inputA: String, inputB: String): String = { var posA = 0 var posB = 0 val sb = new StringBuilder(inputA.length + inputB.length)

      while (posA < inputA.length || posB < inputB.length) {
        val nextA = {
          if (posA < inputA.length) Some(inputA(posA)) else None
        }
        val nextB = {
          if (posB < inputB.length) Some(inputB(posB)) else None
        }
    
        (nextA, nextB) match {
          case (Some(a), Some(b)) => {
            if (a == b) {
    
              val limit = min(inputA.length - posA, inputB.length - posB)
    
              def ALessB(): Boolean = {
                var i = 0
                while (i<limit && (inputA(posA + i) == inputB(posB + i))){
                  i += 1
                }
                if (i == limit) {
                  (inputA.length - posA) > (inputB.length - posB) // take the longer string if equal
                } else {
                  inputA(posA + i) < inputB(posB + i)
                }
              }
    
              def nextNotStart(): Int = {
                var i = 0
                while (i < limit && inputA(posA + i) == a && inputB(posB + i) == a) {
                  i+=1
                }
                i
              }
    
              val aOverB = ALessB()
              val takeUntil = nextNotStart()
              sb.append(a.toString * takeUntil)
              if (aOverB) {
                posA += takeUntil
              } else {
                posB += takeUntil
              }
            }
            else if (a < b) {
              posA += 1
              sb.append(a)
            }
            else {
              posB += 1
              sb.append(b)
            }
          }
          case (Some(a), None) => {
            posA += 1
            sb.append(a)
          }
          case (None, Some(b)) => {
            posB += 1
            sb.append(b)
          }
          case (None, None) => {
          }
        }
      }
      sb.toString()
    }
    
  • + 1 comment

    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

      Optimized String Comparison: The compare function is designed to handle cases where characters from both strings are equal. This function looks ahead to decide the correct sequence to maintain the lexicographical order.

      Efficient Memory Management: The result string's memory is reserved upfront to avoid frequent reallocation, improving performance.

      Handling Ties: When characters are equal, the program checks further along the strings to determine which string to continue with, ensuring the smallest lexicographical result.

  • + 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