diff --git a/Others/Armstrong.java b/Maths/Armstrong.java similarity index 98% rename from Others/Armstrong.java rename to Maths/Armstrong.java index 10999328..60fd8b40 100644 --- a/Others/Armstrong.java +++ b/Maths/Armstrong.java @@ -1,4 +1,4 @@ -package Others; +package strings; /** * An Armstrong number is equal to the sum of the cubes of its digits. diff --git a/Others/Abecedarian.java b/Others/Abecedarian.java deleted file mode 100644 index 13c9b728..00000000 --- a/Others/Abecedarian.java +++ /dev/null @@ -1,24 +0,0 @@ -package Others; - -/** - * An Abecadrian is a word where each letter is in alphabetical order - * - * @author Oskar Enmalm - */ -class Abecedarian { - - public static boolean isAbecedarian(String s) { - int index = s.length() - 1; - - for (int i = 0; i < index; i++) { - - if (s.charAt(i) <= s.charAt(i + 1)) { - } //Need to check if each letter for the whole word is less than the one before it - - else { - return false; - } - } - return true; - } -} diff --git a/Others/Palindrome.java b/Others/Palindrome.java deleted file mode 100644 index af4a72fd..00000000 --- a/Others/Palindrome.java +++ /dev/null @@ -1,50 +0,0 @@ -package Others; - -class Palindrome { - - private String reverseString(String x) { // *helper method - StringBuilder output = new StringBuilder(x); - return output.reverse().toString(); - } - - public boolean FirstWay(String x) { // *palindrome method, returns true if palindrome - if (x == null || x.length() <= 1) - return true; - return x.equalsIgnoreCase(reverseString(x)); - } - - public boolean SecondWay(String x) { - if (x.length() == 0 || x.length() == 1) - return true; - - if (x.charAt(0) != x.charAt(x.length() - 1)) - return false; - - return SecondWay(x.substring(1, x.length() - 1)); - } - - /** - * This method ignores all non-alphanumeric characters and case runs in O(n) - * where n is the length of s - * - * @param s String to check - * @return true if s is palindrome else false - */ - public boolean isPalindrome(String s) { - s = s.toLowerCase().trim(); - StringBuilder sb = new StringBuilder(); - for (char c : s.toCharArray()) { - if (Character.isLetter(c) || Character.isDigit(c)) - sb.append(c); - } - s = sb.toString(); - int start = 0; - int end = s.length() - 1; - while (start <= end) { - if (s.charAt(start++) != s.charAt(end--)) - return false; - - } - return true; - } -} diff --git a/Others/ReverseString.java b/Others/ReverseString.java deleted file mode 100644 index 76bbfb5d..00000000 --- a/Others/ReverseString.java +++ /dev/null @@ -1,46 +0,0 @@ -package Others; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; - -/** - * This method produces a reversed version of a string - * - * @author Unknown - */ -public class ReverseString { - - /** - * This method reverses the string str and returns it - * - * @param str String to be reversed - * @return Reversed string - */ - public static String reverse(String str) { - if (str == null || str.isEmpty()) return str; - - char[] arr = str.toCharArray(); - for (int i = 0, j = str.length() - 1; i < j; i++, j--) { - char temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; - } - return new String(arr); - } - - /** - * Main Method - * - * @param args Command line arguments - * @throws IOException Exception thrown because of BufferedReader - */ - public static void main(String[] args) throws IOException { - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - System.out.println("Enter the string"); - String srr = br.readLine(); - System.out.println("Reverse=" + reverse(srr)); - br.close(); - } -} - diff --git a/strings/Alphabetical.java b/strings/Alphabetical.java new file mode 100644 index 00000000..babf542b --- /dev/null +++ b/strings/Alphabetical.java @@ -0,0 +1,35 @@ +package strings; + +/** + *

+ * Alphabetical order is a system whereby character strings are placed in order + * based on the position of the characters in the conventional ordering of an alphabet. + *

+ * Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order + */ +class Alphabetical { + + public static void main(String[] args) { + assert !isAlphabetical("123abc"); + assert isAlphabetical("aBC"); + assert isAlphabetical("abc"); + assert !isAlphabetical("xyzabc"); + assert isAlphabetical("abcxyz"); + } + + /** + * Check if a string is alphabetical order or not + * + * @param s a string + * @return {@code true} if given string is alphabetical order, otherwise {@code false} + */ + public static boolean isAlphabetical(String s) { + s = s.toLowerCase(); + for (int i = 0; i < s.length() - 1; ++i) { + if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <= s.charAt(i + 1))) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/strings/Palindrome.java b/strings/Palindrome.java new file mode 100644 index 00000000..8f95a83a --- /dev/null +++ b/strings/Palindrome.java @@ -0,0 +1,68 @@ +package strings; + +/** + * Wikipedia: https://en.wikipedia.org/wiki/Palindrome + */ +class Palindrome { + + /** + * Driver Code + */ + public static void main(String[] args) { + String[] palindromes = {null, "", "aba", "123321"}; + for (String s : palindromes) { + assert isPalindrome(s) && isPalindromeRecursion(s) && isPalindrome1(s); + } + + String[] notPalindromes = {"abb", "abc", "abc123"}; + for (String s : notPalindromes) { + assert !isPalindrome(s) && !isPalindromeRecursion(s) && !isPalindrome1(s); + } + } + + /** + * Check if a string is palindrome string or not + * + * @param s a string to check + * @return {@code true} if given string is palindrome, otherwise {@code false} + */ + public static boolean isPalindrome(String s) { + return (s == null || s.length() <= 1) || s.equals(new StringBuilder(s).reverse().toString()); + } + + /** + * Check if a string is palindrome string or not using recursion + * + * @param s a string to check + * @return {@code true} if given string is palindrome, otherwise {@code false} + */ + public static boolean isPalindromeRecursion(String s) { + if (s == null || s.length() <= 1) { + return true; + } + + if (s.charAt(0) != s.charAt(s.length() - 1)) { + return false; + } + + return isPalindrome(s.substring(1, s.length() - 1)); + } + + /** + * Check if a string is palindrome string or not another way + * + * @param s a string to check + * @return {@code true} if given string is palindrome, otherwise {@code false} + */ + public static boolean isPalindrome1(String s) { + if (s == null || s.length() <= 1) { + return true; + } + for (int i = 0, j = s.length() - 1; i < j; ++i, --j) { + if (s.charAt(i) != s.charAt(j)) { + return false; + } + } + return true; + } +} diff --git a/strings/ReverseString.java b/strings/ReverseString.java new file mode 100644 index 00000000..71191731 --- /dev/null +++ b/strings/ReverseString.java @@ -0,0 +1,44 @@ +package strings; + +/** + * Reverse String using different version + */ +public class ReverseString { + + public static void main(String[] args) { + assert reverse("abc123").equals("321cba"); + assert reverse2("abc123").equals("321cba"); + } + + /** + * easiest way to reverses the string str and returns it + * + * @param str string to be reversed + * @return reversed string + */ + public static String reverse(String str) { + return new StringBuilder(str).reverse().toString(); + } + + /** + * second way to reverses the string str and returns it + * + * @param str string to be reversed + * @return reversed string + */ + public static String reverse2(String str) { + + if (str == null || str.isEmpty()) { + return str; + } + + char[] value = str.toCharArray(); + for (int i = 0, j = str.length() - 1; i < j; i++, j--) { + char temp = value[i]; + value[i] = value[j]; + value[j] = temp; + } + return new String(value); + } + +} \ No newline at end of file