Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

I saw the same sub-problem in LeetCode, and there exists a O(n) neat greedy solution:

for _ in range(int(input())):
    a = input() + '['
    b = input() + '['

    output = ""
    for _ in range(len(a) + len(b) - 2):
        if a < b:
            output += a[0]
            a = a[1:]
        else: 
            output += b[0]
            b = b[1:]
      
    print(output)

Please note: '[' is the first char after 'Z'. 

posted on 2016-04-08 03:13  Tonix  阅读(309)  评论(0编辑  收藏  举报