From 6356db0b9771902ea3deb9f1469326f5453bdc87 Mon Sep 17 00:00:00 2001 From: shellhub Date: Wed, 26 Aug 2020 00:05:11 +0800 Subject: [PATCH 1/3] * rename file * create strings directory * fix docs * add test --- {Others => Maths}/Armstrong.java | 2 +- Others/Abecedarian.java | 24 ----------- Others/Palindrome.java | 50 ----------------------- Others/ReverseString.java | 46 --------------------- strings/Alphabetical.java | 35 ++++++++++++++++ strings/Palindrome.java | 68 ++++++++++++++++++++++++++++++++ strings/ReverseString.java | 44 +++++++++++++++++++++ 7 files changed, 148 insertions(+), 121 deletions(-) rename {Others => Maths}/Armstrong.java (98%) delete mode 100644 Others/Abecedarian.java delete mode 100644 Others/Palindrome.java delete mode 100644 Others/ReverseString.java create mode 100644 strings/Alphabetical.java create mode 100644 strings/Palindrome.java create mode 100644 strings/ReverseString.java diff --git a/Others/Armstrong.java b/Maths/Armstrong.java similarity index 98% rename from Others/Armstrong.java rename to Maths/Armstrong.java index 109993280..60fd8b40a 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 13c9b7286..000000000 --- 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 af4a72fde..000000000 --- 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 76bbfb5da..000000000 --- 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 000000000..babf542bf --- /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 000000000..8f95a83a0 --- /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 000000000..71191731f --- /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 From 1d0bc46551ea522088d890c391731bf5d9c31c69 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 25 Aug 2020 16:06:31 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index c90502c70..6abc2b576 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -120,6 +120,7 @@ * [AliquotSum](https://github.com/TheAlgorithms/Java/blob/master/Maths/AliquotSum.java) * [AmicableNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/AmicableNumber.java) * [Area](https://github.com/TheAlgorithms/Java/blob/master/Maths/Area.java) + * [Armstrong](https://github.com/TheAlgorithms/Java/blob/master/Maths/Armstrong.java) * [Average](https://github.com/TheAlgorithms/Java/blob/master/Maths/Average.java) * [Ceil](https://github.com/TheAlgorithms/Java/blob/master/Maths/Ceil.java) * [Combinations](https://github.com/TheAlgorithms/Java/blob/master/Maths/Combinations.java) @@ -162,8 +163,6 @@ ## Others * [3 sum](https://github.com/TheAlgorithms/Java/blob/master/Others/3%20sum.java) - * [Abecedarian](https://github.com/TheAlgorithms/Java/blob/master/Others/Abecedarian.java) - * [Armstrong](https://github.com/TheAlgorithms/Java/blob/master/Others/Armstrong.java) * [BestFit](https://github.com/TheAlgorithms/Java/blob/master/Others/BestFit.java) * [BrianKernighanAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/Others/BrianKernighanAlgorithm.java) * [CountChar](https://github.com/TheAlgorithms/Java/blob/master/Others/CountChar.java) @@ -181,7 +180,6 @@ * [Krishnamurthy](https://github.com/TheAlgorithms/Java/blob/master/Others/Krishnamurthy.java) * [LinearCongruentialGenerator](https://github.com/TheAlgorithms/Java/blob/master/Others/LinearCongruentialGenerator.java) * [LowestBasePalindrome](https://github.com/TheAlgorithms/Java/blob/master/Others/LowestBasePalindrome.java) - * [Palindrome](https://github.com/TheAlgorithms/Java/blob/master/Others/Palindrome.java) * [PasswordGen](https://github.com/TheAlgorithms/Java/blob/master/Others/PasswordGen.java) * [PerlinNoise](https://github.com/TheAlgorithms/Java/blob/master/Others/PerlinNoise.java) * [PowerOfTwoOrNot](https://github.com/TheAlgorithms/Java/blob/master/Others/PowerOfTwoOrNot.java) @@ -190,7 +188,6 @@ * [RemoveDuplicateFromString](https://github.com/TheAlgorithms/Java/blob/master/Others/RemoveDuplicateFromString.java) * [ReturnSubsequence](https://github.com/TheAlgorithms/Java/blob/master/Others/ReturnSubsequence.java) * [ReverseStackUsingRecursion](https://github.com/TheAlgorithms/Java/blob/master/Others/ReverseStackUsingRecursion.java) - * [ReverseString](https://github.com/TheAlgorithms/Java/blob/master/Others/ReverseString.java) * [RootPrecision](https://github.com/TheAlgorithms/Java/blob/master/Others/RootPrecision.java) * [Rotation of array without using extra space](https://github.com/TheAlgorithms/Java/blob/master/Others/Rotation%20of%20array%20without%20using%20extra%20space.java) * [SieveOfEratosthenes](https://github.com/TheAlgorithms/Java/blob/master/Others/SieveOfEratosthenes.java) @@ -235,3 +232,8 @@ * [ShellSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/ShellSort.java) * [SortAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/Sorts/SortAlgorithm.java) * [SortUtils](https://github.com/TheAlgorithms/Java/blob/master/Sorts/SortUtils.java) + +## strings + * [Alphabetical](https://github.com/TheAlgorithms/Java/blob/master/strings/Alphabetical.java) + * [Palindrome](https://github.com/TheAlgorithms/Java/blob/master/strings/Palindrome.java) + * [ReverseString](https://github.com/TheAlgorithms/Java/blob/master/strings/ReverseString.java) From 94bb2088be676fc152138b460afddd8ed063c13a Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Wed, 26 Aug 2020 00:12:00 +0800 Subject: [PATCH 3/3] Update Maths/Armstrong.java --- Maths/Armstrong.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/Armstrong.java b/Maths/Armstrong.java index 60fd8b40a..e1e108473 100644 --- a/Maths/Armstrong.java +++ b/Maths/Armstrong.java @@ -1,4 +1,4 @@ -package strings; +package Maths; /** * An Armstrong number is equal to the sum of the cubes of its digits.