mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Formatted with Google Java Formatter
This commit is contained in:
@@ -1,35 +1,33 @@
|
||||
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
|
||||
* 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");
|
||||
}
|
||||
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;
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,28 +2,26 @@ package strings;
|
||||
|
||||
public class CharactersSame {
|
||||
|
||||
/**
|
||||
* Driver Code
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
assert isAllCharactersSame("");
|
||||
assert !isAllCharactersSame("aab");
|
||||
assert isAllCharactersSame("aaa");
|
||||
assert isAllCharactersSame("11111");
|
||||
}
|
||||
/** Driver Code */
|
||||
public static void main(String[] args) {
|
||||
assert isAllCharactersSame("");
|
||||
assert !isAllCharactersSame("aab");
|
||||
assert isAllCharactersSame("aaa");
|
||||
assert isAllCharactersSame("11111");
|
||||
}
|
||||
|
||||
/**
|
||||
* check if all the characters of a string are same
|
||||
*
|
||||
* @param s the string to check
|
||||
* @return {@code true} if all characters of a string are same, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isAllCharactersSame(String s) {
|
||||
for (int i = 1, length = s.length(); i < length; ++i) {
|
||||
if (s.charAt(i) != s.charAt(0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
/**
|
||||
* check if all the characters of a string are same
|
||||
*
|
||||
* @param s the string to check
|
||||
* @return {@code true} if all characters of a string are same, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isAllCharactersSame(String s) {
|
||||
for (int i = 1, length = s.length(); i < length; ++i) {
|
||||
if (s.charAt(i) != s.charAt(0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,30 +3,30 @@ package strings;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Two strings are anagrams if they are made of the same letters
|
||||
* arranged differently (ignoring the case).
|
||||
* Two strings are anagrams if they are made of the same letters arranged differently (ignoring the
|
||||
* case).
|
||||
*/
|
||||
public class CheckAnagrams {
|
||||
public static void main(String[] args) {
|
||||
assert isAnagrams("Silent", "Listen");
|
||||
assert isAnagrams("This is a string", "Is this a string");
|
||||
assert !isAnagrams("There", "Their");
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
assert isAnagrams("Silent", "Listen");
|
||||
assert isAnagrams("This is a string", "Is this a string");
|
||||
assert !isAnagrams("There", "Their");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if two strings are anagrams or not
|
||||
*
|
||||
* @param s1 the first string
|
||||
* @param s2 the second string
|
||||
* @return {@code true} if two string are anagrams, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isAnagrams(String s1, String s2) {
|
||||
s1 = s1.toLowerCase();
|
||||
s2 = s2.toLowerCase();
|
||||
char[] values1 = s1.toCharArray();
|
||||
char[] values2 = s2.toCharArray();
|
||||
Arrays.sort(values1);
|
||||
Arrays.sort(values2);
|
||||
return new String(values1).equals(new String(values2));
|
||||
}
|
||||
/**
|
||||
* Check if two strings are anagrams or not
|
||||
*
|
||||
* @param s1 the first string
|
||||
* @param s2 the second string
|
||||
* @return {@code true} if two string are anagrams, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isAnagrams(String s1, String s2) {
|
||||
s1 = s1.toLowerCase();
|
||||
s2 = s2.toLowerCase();
|
||||
char[] values1 = s1.toCharArray();
|
||||
char[] values2 = s2.toCharArray();
|
||||
Arrays.sort(values1);
|
||||
Arrays.sort(values2);
|
||||
return new String(values1).equals(new String(values2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,29 +2,27 @@ package strings;
|
||||
|
||||
public class Lower {
|
||||
|
||||
/**
|
||||
* Driver Code
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String[] strings = {"ABC", "ABC123", "abcABC", "abc123ABC"};
|
||||
for (String s : strings) {
|
||||
assert toLowerCase(s).equals(s.toLowerCase());
|
||||
}
|
||||
/** Driver Code */
|
||||
public static void main(String[] args) {
|
||||
String[] strings = {"ABC", "ABC123", "abcABC", "abc123ABC"};
|
||||
for (String s : strings) {
|
||||
assert toLowerCase(s).equals(s.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts all of the characters in this {@code String} to lower case
|
||||
*
|
||||
* @param s the string to convert
|
||||
* @return the {@code String}, converted to lowercase.
|
||||
*/
|
||||
public static String toLowerCase(String s) {
|
||||
char[] values = s.toCharArray();
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
if (Character.isLetter(values[i]) && Character.isUpperCase(values[i])) {
|
||||
values[i] = Character.toLowerCase(values[i]);
|
||||
}
|
||||
}
|
||||
return new String(values);
|
||||
/**
|
||||
* Converts all of the characters in this {@code String} to lower case
|
||||
*
|
||||
* @param s the string to convert
|
||||
* @return the {@code String}, converted to lowercase.
|
||||
*/
|
||||
public static String toLowerCase(String s) {
|
||||
char[] values = s.toCharArray();
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
if (Character.isLetter(values[i]) && Character.isUpperCase(values[i])) {
|
||||
values[i] = Character.toLowerCase(values[i]);
|
||||
}
|
||||
}
|
||||
return new String(values);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,68 +1,64 @@
|
||||
package strings;
|
||||
|
||||
/**
|
||||
* Wikipedia: https://en.wikipedia.org/wiki/Palindrome
|
||||
*/
|
||||
/** 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);
|
||||
}
|
||||
/** Driver Code */
|
||||
public static void main(String[] args) {
|
||||
String[] palindromes = {null, "", "aba", "123321"};
|
||||
for (String s : palindromes) {
|
||||
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());
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
if (s.charAt(0) != s.charAt(s.length() - 1)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +1,35 @@
|
||||
package strings;
|
||||
|
||||
/**
|
||||
* Wikipedia: https://en.wikipedia.org/wiki/Pangram
|
||||
*/
|
||||
/** Wikipedia: https://en.wikipedia.org/wiki/Pangram */
|
||||
public class Pangram {
|
||||
|
||||
/**
|
||||
* Driver Code
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
assert isPangram("The quick brown fox jumps over the lazy dog");
|
||||
assert !isPangram("The quick brown fox jumps over the azy dog"); /* not exists l character */
|
||||
/** Driver Code */
|
||||
public static void main(String[] args) {
|
||||
assert isPangram("The quick brown fox jumps over the lazy dog");
|
||||
assert !isPangram("The quick brown fox jumps over the azy dog"); /* not exists l character */
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string is a pangram string or not
|
||||
*
|
||||
* @param s string to check
|
||||
* @return {@code true} if given string is pangram, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isPangram(String s) {
|
||||
boolean[] marked = new boolean[26]; /* by default all letters don't exists */
|
||||
char[] values = s.toCharArray();
|
||||
for (char value : values) {
|
||||
if (Character.isLetter(value)) {
|
||||
int index = Character.isUpperCase(value) ? value - 'A' : value - 'a';
|
||||
marked[index] = true; /* mark current character exists */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string is a pangram string or not
|
||||
*
|
||||
* @param s string to check
|
||||
* @return {@code true} if given string is pangram, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isPangram(String s) {
|
||||
boolean[] marked = new boolean[26]; /* by default all letters don't exists */
|
||||
char[] values = s.toCharArray();
|
||||
for (char value : values) {
|
||||
if (Character.isLetter(value)) {
|
||||
int index = Character.isUpperCase(value) ? value - 'A' : value - 'a';
|
||||
marked[index] = true; /* mark current character exists */
|
||||
}
|
||||
}
|
||||
|
||||
for (boolean b : marked) {
|
||||
if (!b) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
for (boolean b : marked) {
|
||||
if (!b) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,41 @@
|
||||
package strings;
|
||||
|
||||
/**
|
||||
* Reverse String using different version
|
||||
*/
|
||||
/** Reverse String using different version */
|
||||
public class ReverseString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
assert reverse("abc123").equals("321cba");
|
||||
assert reverse2("abc123").equals("321cba");
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
return new String(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,58 @@
|
||||
package strings;
|
||||
|
||||
|
||||
/**
|
||||
* Given a string, moving several characters
|
||||
* in front of the string to the end of the string.
|
||||
* For example, move the two characters'a' and 'b' in
|
||||
* front of the string "abcdef" to the end of the string,
|
||||
* so that the original string becomes the string "cdefab"
|
||||
* Given a string, moving several characters in front of the string to the end of the string. For
|
||||
* example, move the two characters'a' and 'b' in front of the string "abcdef" to the end of the
|
||||
* string, so that the original string becomes the string "cdefab"
|
||||
*/
|
||||
public class Rotation {
|
||||
public static void main(String[] args) {
|
||||
assert rotation("abcdef", 2).equals("cdefab");
|
||||
public static void main(String[] args) {
|
||||
assert rotation("abcdef", 2).equals("cdefab");
|
||||
|
||||
char[] values = "abcdef".toCharArray();
|
||||
rotation(values, 2);
|
||||
assert new String(values).equals("cdefab");
|
||||
char[] values = "abcdef".toCharArray();
|
||||
rotation(values, 2);
|
||||
assert new String(values).equals("cdefab");
|
||||
}
|
||||
|
||||
/**
|
||||
* Move {@code n} characters in front of given string to the end of string time complexity: O(n)
|
||||
* space complexity: O(n)
|
||||
*
|
||||
* @param s given string
|
||||
* @param n the total characters to be moved
|
||||
* @return string after rotation
|
||||
*/
|
||||
public static String rotation(String s, int n) {
|
||||
return s.substring(n) + s.substring(0, n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move {@code n} characters in front of given character array to the end of array time
|
||||
* complexity: O(n) space complexity: O(1)
|
||||
*
|
||||
* @param values given character array
|
||||
* @param n the total characters to be moved
|
||||
*/
|
||||
public static void rotation(char[] values, int n) {
|
||||
reverse(values, 0, n - 1);
|
||||
reverse(values, n, values.length - 1);
|
||||
reverse(values, 0, values.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse character array
|
||||
*
|
||||
* @param values character array
|
||||
* @param from begin index of given array
|
||||
* @param to end index of given array
|
||||
*/
|
||||
public static void reverse(char[] values, int from, int to) {
|
||||
while (from < to) {
|
||||
char temp = values[from];
|
||||
values[from] = values[to];
|
||||
values[to] = temp;
|
||||
from++;
|
||||
to--;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move {@code n} characters in front of given string to the end of string
|
||||
* time complexity: O(n)
|
||||
* space complexity: O(n)
|
||||
*
|
||||
* @param s given string
|
||||
* @param n the total characters to be moved
|
||||
* @return string after rotation
|
||||
*/
|
||||
public static String rotation(String s, int n) {
|
||||
return s.substring(n) + s.substring(0, n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move {@code n} characters in front of given character array to the end of array
|
||||
* time complexity: O(n)
|
||||
* space complexity: O(1)
|
||||
*
|
||||
* @param values given character array
|
||||
* @param n the total characters to be moved
|
||||
*/
|
||||
public static void rotation(char[] values, int n) {
|
||||
reverse(values, 0, n - 1);
|
||||
reverse(values, n, values.length - 1);
|
||||
reverse(values, 0, values.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse character array
|
||||
*
|
||||
* @param values character array
|
||||
* @param from begin index of given array
|
||||
* @param to end index of given array
|
||||
*/
|
||||
public static void reverse(char[] values, int from, int to) {
|
||||
while (from < to) {
|
||||
char temp = values[from];
|
||||
values[from] = values[to];
|
||||
values[to] = temp;
|
||||
from++;
|
||||
to--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,29 +2,27 @@ package strings;
|
||||
|
||||
public class Upper {
|
||||
|
||||
/**
|
||||
* Driver Code
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String[] strings = {"ABC", "ABC123", "abcABC", "abc123ABC"};
|
||||
for (String s : strings) {
|
||||
assert toUpperCase(s).equals(s.toUpperCase());
|
||||
}
|
||||
/** Driver Code */
|
||||
public static void main(String[] args) {
|
||||
String[] strings = {"ABC", "ABC123", "abcABC", "abc123ABC"};
|
||||
for (String s : strings) {
|
||||
assert toUpperCase(s).equals(s.toUpperCase());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts all of the characters in this {@code String} to upper case
|
||||
*
|
||||
* @param s the string to convert
|
||||
* @return the {@code String}, converted to uppercase.
|
||||
*/
|
||||
public static String toUpperCase(String s) {
|
||||
char[] values = s.toCharArray();
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
if (Character.isLetter(values[i]) && Character.isLowerCase(values[i])) {
|
||||
values[i] = Character.toUpperCase(values[i]);
|
||||
}
|
||||
}
|
||||
return new String(values);
|
||||
/**
|
||||
* Converts all of the characters in this {@code String} to upper case
|
||||
*
|
||||
* @param s the string to convert
|
||||
* @return the {@code String}, converted to uppercase.
|
||||
*/
|
||||
public static String toUpperCase(String s) {
|
||||
char[] values = s.toCharArray();
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
if (Character.isLetter(values[i]) && Character.isLowerCase(values[i])) {
|
||||
values[i] = Character.toUpperCase(values[i]);
|
||||
}
|
||||
}
|
||||
return new String(values);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user