Skip to content

Instantly share code, notes, and snippets.

@nguyen-thom
Created May 24, 2018 02:26
Show Gist options
  • Save nguyen-thom/b88e5764e189917b823f6af3df10da79 to your computer and use it in GitHub Desktop.
Save nguyen-thom/b88e5764e189917b823f6af3df10da79 to your computer and use it in GitHub Desktop.

Revisions

  1. nguyen-thom created this gist May 24, 2018.
    170 changes: 170 additions & 0 deletions BT.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    package main;

    import java.util.Arrays;

    public class BT {
    final static String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    final static char[] CHAR_ALPHABETS_UPPSER = ALPHABET.toCharArray();
    final static char[] CHAR_ALPHABETS_LOWER = ALPHABET.toLowerCase()
    .toCharArray();

    /**
    * encrypt origin string with turn alphabet.
    *
    * @param origin
    * @param k
    * @return
    */
    public static String encode(String origin, int k) {
    char[] toEncode = origin.toCharArray();
    for (int i = 0; i < toEncode.length; i++) {
    if (Character.isLetter(toEncode[i])) {
    int index = i - k;
    if (index < 0) {
    index = toEncode[i] + 25;
    }
    if (index > 26) {
    index = toEncode[i] - 26;
    }
    toEncode[i] -= k;
    toEncode[i] = (char) ((toEncode[i] - 3 - (int) 'A') % 26 + (int) 'a');
    }
    }
    origin = String.valueOf(toEncode);
    return origin;
    }

    /**
    * ham nay khong phan biet duoc chu thuong / chu hoa. tat ca chu sau khi
    * giai ma la chu thuong
    *
    * @param encrypt
    * @param k
    * @return
    * @throws Exception
    */
    public static String decode(String encrypt, int k) throws Exception {
    // kiem tra gia tri dau vao
    if (encrypt == null) {
    System.out.println("Can't decode null value");
    throw new RuntimeException("Can't decode null value");
    }
    // kiem tra gia tri dau vao cho K
    if (k < 0 || k >= 25) {
    System.out.println("Can't decode with k invalid");
    throw new RuntimeException("Can't decode with k invalid");
    }
    if (encrypt.isEmpty()) {
    return "";
    }
    // ------- bat dau decode (giai ma)--------
    char[] encryptChars = encrypt.toUpperCase().toCharArray();
    char[] decryptChars = new char[encrypt.length()];
    for (int i = 0; i < encryptChars.length; ++i) {
    char afterDecodeChar = encryptChars[i];
    // only decode with letter character.
    if (Character.isLetter(afterDecodeChar)) {
    // find index of character in alphabet
    int foundIndex = 0;
    // co the thay the ham nay bang tim kiem trong vong for
    foundIndex = Arrays.binarySearch(CHAR_ALPHABETS_UPPSER,
    afterDecodeChar);
    // for (int index = 0; index < CHAR_ALPHABETS.length; ++index) {
    // if (afterDecodeChar == CHAR_ALPHABETS[index]) {
    // foundIndex = index;
    // break;
    // }
    // }
    // after find index of encrypt
    foundIndex -= k;
    // xoay vong nguoc kim dong ho BAZYX...
    if (foundIndex < 0) {
    foundIndex = CHAR_ALPHABETS_UPPSER.length + foundIndex;
    }
    // xoay vong cung chieu kim dong ho
    // khong co truong hop lon hon 25. vi max length = 25 roi.
    // if (foundIndex > 25) {
    // foundIndex = CHAR_ALPHABETS.length - foundIndex;
    // }
    afterDecodeChar = CHAR_ALPHABETS_UPPSER[foundIndex];

    }
    // add value to result;
    decryptChars[i] = afterDecodeChar;
    }

    return String.copyValueOf(decryptChars).toLowerCase();
    }

    /**
    * ham nay phan biet duoc chu hoa chu thuong
    *
    * @param encrypt
    * @param k
    * @return
    * @throws Exception
    */
    public static String decodeUpLowCase(String encrypt, int k)
    throws Exception {
    // kiem tra gia tri dau vao
    if (encrypt == null) {
    System.out.println("Can't decode null value");
    throw new RuntimeException("Can't decode null value");
    }
    // kiem tra gia tri dau vao cho K
    if (k < 0 || k >= 25) {
    System.out.println("Can't decode with k invalid");
    throw new RuntimeException("Can't decode with k invalid");
    }
    if (encrypt.isEmpty()) {
    return "";
    }
    // ------- bat dau decode (giai ma)--------
    char[] encryptChars = encrypt.toCharArray();
    char[] decryptChars = new char[encrypt.length()];
    for (int i = 0; i < encryptChars.length; ++i) {
    char afterDecodeChar = encryptChars[i];
    // only decode with letter character.
    if (Character.isLetter(afterDecodeChar)) {
    char[] searchChar = CHAR_ALPHABETS_UPPSER;
    if (Character.isLowerCase(afterDecodeChar)) {
    searchChar = CHAR_ALPHABETS_LOWER;
    }
    int foundIndex = Arrays.binarySearch(searchChar,
    afterDecodeChar);
    // find index of character in alphabet

    // tru nguoc di k vi tri
    foundIndex -= k;
    // xoay vong nguoc kim dong ho BAZYX...
    if (foundIndex < 0) {
    foundIndex = ALPHABET.length() + foundIndex;
    }
    afterDecodeChar = searchChar[foundIndex];
    }
    // add value to result;
    decryptChars[i] = afterDecodeChar;
    }

    return String.copyValueOf(decryptChars);
    }

    public static void main(String[] args) throws Exception {
    // test 1
    String encrypt1 = "ab";
    System.out.println("After decode 1: " + BT.decode(encrypt1, 2));

    // test 2
    String encrypt2 = "123xyz";
    System.out.println("After decode 2: " + BT.decode(encrypt2, 2));

    // test 3
    String encrypt3 = "%^&***^&xyz";
    System.out.println("After decode 3: " + BT.decode(encrypt3, 2));

    // test 4 // test chu hoa chu thuong
    String encrypt4 = "Ab";
    System.out
    .println("After decode 4: " + BT.decodeUpLowCase(encrypt4, 2));
    }
    }