* rename file
* create strings directory * fix docs * add test
This commit is contained in:
parent
f90a8bed7e
commit
6356db0b97
@ -1,4 +1,4 @@
|
||||
package Others;
|
||||
package strings;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user