import java.math.BigInteger;
import java.util.*;

/**
 * NC359 大数相减
 * @author d3y1
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param num1 string字符串
     * @param num2 string字符串
     * @return string字符串
     */
    public String substring (String num1, String num2) {
        return solution1(num1, num2);
        // return solution2(num1, num2);
    }

    /**
     * 模拟法
     * @param num1
     * @param num2
     * @return
     */
    private String solution1(String num1, String num2){
        boolean isNegative = false;
        if(num1.length() < num2.length()){
            isNegative = true;
        }else if(num1.length() == num2.length()){
            for(int i=0; i<num1.length(); i++){
                if(num1.charAt(i) < num2.charAt(i)){
                    isNegative = true;
                    break;
                }
            }
        }

        if(isNegative){
            String tmp = num1;
            num1 = num2;
            num2 = tmp;
        }

        int len1 = num1.length();
        int len2 = num2.length();
        char[] num1Chars = num1.toCharArray();
        char[] num2Chars = num2.toCharArray();
        int digit1,digit2;
        int digit;
        StringBuilder sb = new StringBuilder();
        for(int i=len1-1,j=len2-1; i>=0; i--,j--){
            digit1 = num1Chars[i]-'0';
            digit2 = j>=0?num2Chars[j]-'0':0;

            if(digit1 >= digit2){
                digit = digit1 - digit2;
            }else{
                // 向高位借1
                int hDigit;
                for(int k=i-1; k>=0; k--){
                    hDigit = num1Chars[k]-'0';
                    if(hDigit > 0){
                        num1Chars[k]--;
                        break;
                    }else{
                        num1Chars[k] += 9;
                    }
                }
                digit = digit1 + 10 - digit2;
            }

            sb.append(digit);
        }

        String result = sb.reverse().toString();

        // 为0
        String regex = "0+";
        if(result.matches(regex)){
            return "0";
        }
        // 去掉前缀0
        for(int i=0; i<result.length(); i++){
            if(result.charAt(i) != '0'){
                result = result.substring(i);
                break;
            }
        }
        // 负数
        if(isNegative){
            result = "-"+result;
        }

        return result;
    }

    /**
     * BigInteger()
     * @param num1
     * @param num2
     * @return
     */
    private String solution2(String num1, String num2){
        return new BigInteger(num1).subtract(new BigInteger(num2)).toString();
    }
}