* Lower.java
* Upper.java * Pangram.java * CharactersSame.java * CheckAnagrams.java
This commit is contained in:
parent
19176d9ede
commit
0f4905d1d9
29
strings/CharactersSame.java
Normal file
29
strings/CharactersSame.java
Normal file
@ -0,0 +1,29 @@
|
||||
package strings;
|
||||
|
||||
public class CharactersSame {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
32
strings/CheckAnagrams.java
Normal file
32
strings/CheckAnagrams.java
Normal file
@ -0,0 +1,32 @@
|
||||
package strings;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 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");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
30
strings/Lower.java
Normal file
30
strings/Lower.java
Normal file
@ -0,0 +1,30 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
39
strings/Pangram.java
Normal file
39
strings/Pangram.java
Normal file
@ -0,0 +1,39 @@
|
||||
package strings;
|
||||
|
||||
/**
|
||||
* 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 */
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
30
strings/Upper.java
Normal file
30
strings/Upper.java
Normal file
@ -0,0 +1,30 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user