mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 18:32:56 +08:00
Merge pull request #1436 from shellhub/dev
Created strings directory And rename files
This commit is contained in:
10
DIRECTORY.md
10
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)
|
||||
|
@ -1,4 +1,4 @@
|
||||
package Others;
|
||||
package Maths;
|
||||
|
||||
/**
|
||||
* An Armstrong number is equal to the sum of the cubes of its digits.
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
35
strings/Alphabetical.java
Normal file
35
strings/Alphabetical.java
Normal file
@ -0,0 +1,35 @@
|
||||
package strings;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 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.
|
||||
* </p>
|
||||
* 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;
|
||||
}
|
||||
}
|
68
strings/Palindrome.java
Normal file
68
strings/Palindrome.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
44
strings/ReverseString.java
Normal file
44
strings/ReverseString.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user