Morgan and a String

Sort by

recency

|

313 Discussions

|

  • + 0 comments

    I could pass them all use the following. I have a helper function called fast_get that one will return '[' which is the next ascii charecter after Z in case any of the stacks reached end of index. This way I can make sure even if I get out of bond ot the stacks the loop still works fine.

    `python def fast_get(seq, i, default='['): return seq[i] if 0 <= i < len(seq) else default

    def morganAndString(a, b): # Write your code here a = deque(a) b= deque(b) result="" while a or b: if a and b: i=0

            while fast_get(a,i)==fast_get(b,i) and (i<len(a) or i< len(b)):
                i+=1
                if fast_get(a,i)<fast_get(b,i):
                    for _ in range(i+1):
                        result+= a.popleft()
                    i=0
    
    
                elif fast_get(b,i)<fast_get(a,i):
                    for _ in range(i+1):
                        result+= b.popleft()
                    i=0
            i=0                            
            if fast_get(a,i)<fast_get(b,i):
                result += a.popleft()
    
    
            elif fast_get(b,i)<fast_get(a,i):
                result += b.popleft()
            elif fast_get(b,i)==fast_get(a,i):
                result += a.popleft()                
    
        elif b:
            result += b.popleft()
        elif a:
            result += a.popleft()
    return 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();
    }
    

    }

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