wupangyen / LeetCode

LeetCode
1 stars 0 forks source link

Code in Game Reverse Mode #20

Open wupangyen opened 3 years ago

wupangyen commented 3 years ago
Screen Shot 2021-08-21 at 5 54 35 PM
wupangyen commented 3 years ago

package codeInGame;

public class ReverseMode {

public static void main(String[] args) {
    String s = "(1)00-(0)01-(2)02-(0)03";

    System.out.println(ReverseMode(s));

}

public static String ReverseMode(String s) {
    String[] upper = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"} ;
    String[] lower = new String[]{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    String[] digits = new String[] {"0","1","2","3","4","5","6","7","8","9"};
    String[] split = s.split("-");
    String res = "";
    String num_str = "";
    int num = 0;
    for(String str: split) {

        if(str.charAt(1) == '0') {
            // index 3 and index 4 
            num_str = "" + str.charAt(3) + str.charAt(4);
            num = Integer.parseInt(num_str);
            res += lower[num];
        }
        else if(str.charAt(1) == '1') {
            num_str = "" + str.charAt(3) + str.charAt(4);
            num = Integer.parseInt(num_str);
            res += upper[num];
        }
        else if(str.charAt(1) == '2') {
            num_str = "" + str.charAt(3) + str.charAt(4);
            num = Integer.parseInt(num_str);
            res += digits[num];
        }

    }

    return res;

}

}