Java - Anagram String - Alternate Solution

Jun 23, 2018

This is an alternate solution when verifying if a String is an anagramam. The algoritm: For both Strings: Remove special characters (!,’.) For both Strings: Split the String into array of characters Iterate both arrays and check if they have same size and characters are the same To remove the special characters we can use regex: public class SpecialCharRemover { public static String removeSpecial(String s1){ if(isNull(s1)){ return null; } return s1.replaceAll("[^\\w\\s]",""); } } Creating character map public class CharMap { public static Map<String, Integer> getCharMap(String s){ if(isNull(s)){ return new HashMap<>(); } Map<String, Integer> charMap = new HashMap<>(); for(String curr :...

Java - Anagram String

Jun 23, 2018

Another common Java exercise is to verify if two Strings are anagrams The algoritm is: For both Strings: Remove special characters (!,’.) For both Strings: Split the String into array of characters For both Strings: Create a Character Map, with key = char, value = number of occurences of char Iterate the array of characters and calculate the CharacterMap Verify if both Maps are equal To remove the special characters we can use regex: public class SpecialCharRemover { public static String removeSpecial(String s1){ if(isNull(s1)){ return null; } return s1.replaceAll("[^\\w\\s]",""); } } Creating character map public class CharMap { public static...

Java - Palindrome String

Jun 22, 2018

Another common Java exercise is to verify if a String is a Palindrome The algorithm is: Split the String into an array with all the characters Reverse the array Join the array again Check if the reversed string is equal to the input Reversing the array public class StringReverser { public static String reverse(String s1){ if(isNull(s1)){ return null; } List<String> charList = asList(s1.split("")); Collections.reverse(charList); return String.join("", charList); } } Palindrome class public class Palindrome { public static boolean isPalindrome(String s1){ if(isNull(s1)){ return false; } String reversed = StringReverser.reverse(s1); return reversed.equals(s1); } } Source code: Palindrome

Java - Reverse String - alternate

Jun 21, 2018

This is an alternate solution for reversing Strings in java The algorithm: Split the String into an array with all the characters Iterate throught the array, starting from the last position and adding into a new string public static String reverse(String s1) { if(isNull(s1)){ return null; } StringBuilder stringBuilder = new StringBuilder(); String[] splitted = s1.split(""); for (int i = splitted.length - 1; i >= 0; i--) { stringBuilder.append(splitted[i]); } return stringBuilder.toString(); } Tests: @Test public void reverse1(){ String entry = "ABCDEF"; String expected = "FEDCBA"; String result = StringReverserByConcat.reverse(entry); assertEquals(expected, result); } @Test public void reverseNull(){ String entry =...

Java - Reverse String

Jun 21, 2018

Reverse a string is a common exercise and this is a simple Java solution The algorithm is: Split the String into an array with all the characters Reverse the array Join the array again public static String reverse(String s1){ if(isNull(s1)){ return null; } List<String> charList = asList(s1.split("")); Collections.reverse(charList); return String.join("", charList); } Tests: @Test public void reverse1(){ String entry = "ABCDEF"; String expected = "FEDCBA"; String result = StringReverser.reverse(entry); assertEquals(expected, result); } @Test public void reverseNull(){ String entry = null; String expected = null; String result = StringReverser.reverse(entry); assertEquals(expected, result); } @Test public void reverseEmpty(){ String entry = ""; String...

Docker Spring boot - Maven

May 22, 2018

This is a tutorial on how to use Docker (Dockerize) a Spring boot application using Maven Follow the official spring tutorial here On the root folder create a new folder with a Dockerfile in it: mkdir docker && touch docker/Dockerfile FROM openjdk:8-jdk EXPOSE 8080 COPY dockerizing-springboot-0.1.0.jar /opt/boot.jar ENTRYPOINT java -jar /opt/boot.jar Add the following lines to pom.xml (inside build -> plugins tag) <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <imageName>springbootsample</imageName> <dockerDirectory>docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> Run the command to build a new image (called springbootsample): mvn clean install docker:build Start the container with the following command: docker run...

Moving Blog

Feb 18, 2018

Moving the blog I am moving the blog to a new server outside github pages I will keep posting here and there as a mirror, let’s see how things go =) The link: New Blog