Skip to content

Instantly share code, notes, and snippets.

@ChocolatMilka
Created November 26, 2020 16:33
Show Gist options
  • Select an option

  • Save ChocolatMilka/ae1cbd40bebf8a271fd25cb57b864109 to your computer and use it in GitHub Desktop.

Select an option

Save ChocolatMilka/ae1cbd40bebf8a271fd25cb57b864109 to your computer and use it in GitHub Desktop.
Save 2 bytes every 6 bytes. 33% saves
public class CompressStr2Int {
public static void main (String[] args) {
System.out.println(generateStr(generateInt("SUNDAY")));
/*
Objective: returned value must be exact to the argument
Status: Successfull
*/
}
public static String generateStr (int i) {
return i < 0 ? "" : generateStr((i / 26) - 1) + (char)(65 + i % 26);
}
public static int generateInt (String str) {
int total = -1; // generateStr() start by 0 and not 1
int size = str.length();
for (int i = 0; i < size; i++) {
int number = ((int) str.charAt(i))-64; // char_65=a | +1 for [1 , 26] range and not [0, 25]
total += number*factor(size-i);
}
return total;
}
private static int factor (int place) { return (place > 1) ? (int) Math.pow(26, place-1) : 1; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment