Friday, 30 September 2022

Leetcode Note

use ascii table to solve question

If string consis of all lower english letter, we can define an array of [26]int. 'a' is 97

Ascii Table

Leetcode questions

integer to binary string

XOR

//try to find x
5^x = 2
5^5^x = 2^5
0^x = 2^5
x=2^5

Linked List

Set (golang does not have set)

Regular Expression

Sort String and remove duplicated characters

Math

Integer boundary

Design

Recursive Memoization

Backtracking

Here is a good explanation for Backtracking. What is Backtracking

One general method to solve combination related backtracking is to use breadth first search tree travesal. I found it is very handy, but may not efficient.

  • use a linked list to store element of every level
  • initiate the first level
  • start a while. Based on the question, decide when to exit the loop
  • inside the while loop, level by level using size and poll

Solution for Leetcode 17

class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> ans = new ArrayList<>();
        if (digits.length()==0) return ans;

        HashMap<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");

        int l = digits.length();

        LinkedList<String> list = new LinkedList<>();

        int h = 0;

        //the first level
        char c = digits.charAt(h);
        String s = map.get(c);
        for (int i=0; i<s.length(); i++) {
            list.add(Character.toString(s.charAt(i)));
        }

        while (h<l-1) {
            h++;
            int size = list.size();
            for (int i=0; i<size; i++) {
                String s1 = list.poll();
                String toAdd = map.get(digits.charAt(h));

                for (int j=0; j<toAdd.length(); j++) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(s1);
                    sb.append(toAdd.charAt(j));
                    list.add(sb.toString());
                }
            }
        }

       
        while (list.size()>0) {
            ans.add(list.poll());
        }

        return ans;
        
    }
}


Check is Parentheses valid

private boolean isValid(String s) {
        int left = 0, right = 0;
        for (int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            if (c=='(') {
                left++;
            } else {
                right++;
            }

            if (right > left) return false;
        }

        return left==right;
    }

Thursday, 8 September 2022

Stop http to https redirect in Chrome

When do development, once a while Chrome starts to do this redirect. Because my local does not have valid certs, then a warning page shows up. The below is how to disable it.

Thursday, 1 September 2022

Braintree Integration and Apple Pay

How drop in UI works

  • Use code provided by Braintree to set up a page
  • Need to add merchant tokenization key in the above code. The key can be found in Braintree console
  • Also need to set the url where ajax can call when the submit button is clicked
  • In server side for that ajax call
    • get paymentMethodNonce such as tokencc_bf_jq8mhh_skhmmj_vf9883_9hzp6y***** which represent payment info customer provided.
    • use paymentMethodNonce to create a payment method
    • use payment method to create a transaction. Of course, that ajax call should also pass along amount and customer info
    • When done, send back response to that ajax call
  • After get ajax call response, we can show receipt page or show error based on response.
//use payment method nonce to generate a payment method. After get payment method, we can create transaction
$result = $gateway->paymentMethod()->create([
            'customerId' => '691651999',
            'paymentMethodNonce' => "tokencc_bf_jq8mhh_skhmmj_vf9883_9hzp6y_xxxxx"
        ]);

How paymentMethod nonce is generated

  • add a click event listener for submit button
  • inside listener, call requestPaymentMethod on the branstree instance
  • the second parameter payload has none. That is payload.nonce
var button = document.querySelector('#submit-button');

            braintree.dropin.create({
                authorization: 'sandbox_7bj5fsqp_j52y8m6kfndzsjr2',
                container: '#dropin-container'
            }, function (createErr, instance) {
                button.addEventListener('click', function () {
                    instance.requestPaymentMethod(function (requestPaymentMethodErr, payload) {
                        // Submit payload.nonce to your server
                        console.log("payload", payload)
                    });
                });
            })  ;

Setting up Apple Pay

Here is some general steps

However, to set up Apple Pay through Braintree, we need to adjust some steps

Debug Javascript of Web page on iPad by mac

  • In iPad, Settings - Safari - Advanced - enable Web Inspector
  • In Mac, go to Safari - Prefences - Advanced - Show Develop menu bar
  • Connect iPad with mac using usb cable
  • In iPad, open the webpag in Safari
  • In mac, open Safari, develop menu bar and find iPad; then click the web page
  • Console log will show up