Match all the letters in string - java

I am trying to find out that if my string contains all the letters from a to z & A to Z. I tried below: but it will return true if it has aA. I am looking for all the 52 letters i.e. if string have all 52 letters then only it will return true else false by using Pattern and matcher.
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
s=s.replaceAll("\\s+", "");
System.out.println(s);
// String input = "[a-zA-Z]+";
String input = "\\w+";
System.out.println(input);
Pattern pr = Pattern.compile(input);
Matcher m =pr.matcher(s);
if(m.matches()){
System.out.println("pangram");
} else {
System.out.println("non-pangram");
}
We can solve this by other ways, but I am trying to solve it by only using Pattern and matcher.

If you want to see if all 52 upper- and lower-case letters are present in a given input string, you can't use a regular expression. It simply cannot do something like that.
If the list of characters that must be present is dynamic, you can use this method:
private static boolean containsAllOf(String input, String alphabet) {
boolean[] found = new boolean[alphabet.length()];
int foundCount = 0;
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
int idx = alphabet.indexOf(ch);
if (idx >= 0 && ! found[idx]) {
found[idx] = true;
if (++foundCount == found.length)
return true;
}
}
return false;
}
E.g. use it like this:
containsAllOf("abc", "abcdef") // returns false
containsAllOf("dfu hadkf kojuhoeuaf", "abcdef") // returns false
containsAllOf("bad fed", "abcdef") // returns false
containsAllOf("bad fec", "abcdef") // returns true
Performance can be improved if you specifically want to check the entire 52 upper- and lower-case letters of the English alphabet.
private static boolean containsAllOfAlphabet(String input) {
boolean[] found = new boolean[52];
int foundCount = 0;
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
int idx = (ch >= 'a' && ch <= 'z' ? ch - 'a' :
ch >= 'A' && ch <= 'Z' ? ch - 'A' + 26 : -1);
if (idx >= 0 && ! found[idx]) {
found[idx] = true;
if (++foundCount == found.length)
return true;
}
}
return false;
}
UPDATE If you want to use a pattern matcher, here is one way.
Start by sorting all the characters of the input string, then use a pattern matcher to eliminate all non-letters and duplicate letters. If the length of the result is 52, then all letters are present.
Of course, a pangram normally don't consider uppercase and lowercase letters to be different, so calling toLowercase() and checking for length 26 may be more correct:
String input = "Pack my box with five dozen liquor jugs.";
char[] buf = input.toLowerCase().toCharArray();
Arrays.sort(buf);
boolean pangram = (new String(buf).replaceAll("[^a-zA-Z]|([a-zA-Z])\\1+", "$1").length() == 26);
System.out.println(pangram ? "pangram" : "non-pangram");

I have used set and putting the every character(ASC) in it, as Set only contains the unique values so I am checking size == 26 for all characters. use if(put>=97 && put<=122 ) before adding.
Scanner sc = new Scanner(System.in);
String s= sc.nextLine();
Set<Integer> h = new HashSet<Integer>();
for (int i = 0; i < s.length(); i++) {
int put = (int)s.charAt(i);
if(put>=97 && put<=122 ) {
h.add(put);
}
if(put>=65 && put<=90 ) {
h.add(put);
}
}
if(h.size()==52)
System.out.println("all 52");
else
System.out.println("missing");

Related

Is it more efficient to use arrays or character?

is this the most efficient method to determine that there is a minimum of two unique characters in a String? Should I have used arrays to hold the characters? Thank you
public static Boolean checkPW(String pw) {
Boolean validLower = false, validUpper = false, validNumber = false;
char lowerCompare = '0', upperCompare = '0', numberCompare = 'a';
for(int position = 0; position < pw.length(); ++position) {
char character = pw.charAt(position);
if(character >= 'a' && character <= 'z') {
if(lowerCompare == '0')
lowerCompare = character;
else if(lowerCompare != character)
validLower = true;
} // lower-case if END
} // for-loop END
if(validLower)
return true;
else
return false;
} // checkPW END
If I had to do this in Java, in production, I might just use a set here:
String input = "abcdefgzz";
char[] letters = input.toCharArray();
Set<Character> set = new HashSet<>();
boolean valid = false;
for (char letter : letters) {
set.add(letter);
if (set.size() > 1) {
valid = true;
break;
}
}
if (valid) {
System.out.println("Minimum of two unique letters");
}
else {
System.out.println("Only one unique letter");
}
is this the most efficient method to determine that there is a minimum of two unique characters in a String?
No. The loop continues to run after 2 unique valid characters are found, which is unnecessary. It could stop immediately, and then it will be more efficient. Consider for example the string "ab" followed by a million characters. There's no need to go further than the first two.
Should I have used arrays to hold the characters?
The question is not clear. To make it meaningful, you would need to include reasoning for the benefits of both methods. And it's not clear what technique you're referring to.
It would be good to remove all the unnecessary variables from the program.
After fixing the inefficiency, and a bit of cleanup:
public static boolean checkPW(String pw) {
char first = '0';
for (int position = 0; position < pw.length(); ++position) {
char character = pw.charAt(position);
if ('a' <= character && character <= 'z') {
if (first == '0') {
first = character;
} else if (first != character)
return true;
}
}
}
return false;
}
I'd do this:
public static boolean checkPW(String pw) {
Character lowerCompare = null;
for (int position = 0; position < pw.length(); ++position) {
char character = pw.charAt(position);
if(Character.isLowerCase(c)) { // this handles non-ASCII lower case characters
if(lowerCompare == null) {
lowerCompare = character;
} else if(lowerCompare != character) {
return true;
}
}
}
return false;
}

How to add new values into existing upperCase to new lowerCase

I am developing a method which takes ArrayList as an argument.
Then, the method makes some changes into the array and returns transformed arrayList.
The input array is going to be like that {A123, C123, 15B2} and I would like to get the following output {Aa123, Cc123, 15Bb2}.
That is to say, after any capital letter I need to add the same lowercase letter.
And there are any order and quantity of letters, e.g. it is also possible to get strings like those Hkjk124, hy71.
The method is shown below:
protected ArrayList<String> enrichValues(ArrayList<String> list) {
for (int i = 0; i < list.size(); i++) {
char[] charArray = list.get(i).toCharArray();
List<Character> listChars = new ArrayList<>();
for (char c : charArray) {
listChars.add(c);
}
for (int j = 0; j < listChars.size(); j++) {
if (listChars.get(j).charValue() == 'A') {
listChars.add(j + 1, 'a');
}
}
String newChar = "";
for (Character c : listChars)
newChar += c.toString();
list.set(i, newChar);
}
return list;
}
The main problem I have faced to is that I do not know how to check if a letter is uppercase.
I failed to apply something like:
if(Character.isLetter(c) && Character.isUpperCase(c)) {
listChars.add(j + 1, 'a');
}
Because of that I have to add lots of checks:
if (listChars.get(j).charValue() == 'B') {
listChars.add(j + 1, 'b');
}
if (listChars.get(j).charValue() == 'C') {
listChars.add(j + 1, 'c');
}
But it is a very bad approach. I would appreciate any help.
Here's a way of doing it that works like a charm :
public static ArrayList<String> enrichValues(ArrayList<String> values){
ArrayList<String> array = new ArrayList<String>();
for (String str : values){ //For each string
StringBuilder copy = new StringBuilder();
for (char c : str.toCharArray()) {//For each char
copy.append(c);
if(Character.isLetter(c) && Character.isUpperCase(c)){
copy.append(Character.toLowerCase(c));
}
}
array.add(copy.toString());
}
return array;
}
Example :
public static void main(String[] args) {
String a = "A123";
String b = "C123";
String c = "15B2";
String d = " Hkjk124";
String e = "hy71";
String g = "AbCdE645 DeeeFFD";
ArrayList<String> values = new ArrayList<String>();
values.add(a);
values.add(b);
values.add(c);
values.add(d);
values.add(e);
values.add(g);
values = enrichValues(values);
System.out.println(values.toString());
}
Output : [Aa123, Cc123, 15Bb2, Hhkjk124, hy71, AabCcdEe645 DdeeeFfFfDd]
When you are writing some method which accepts concrete implementation of the List interface such as ArrayList, consider to change it to List type. This will allow you to pass in any form of list: LinkedList, ArrayList, ...
Another thing you shoud know is, that joining strings via += is inefficient, as it creates new String instance each time += is applied. Instead of doing this, you should use StringBuilder which allocates resizable buffer for string where you can append other characters.
Condition Character.isLetter(c) && Character.isUpperCase(c) is redudant, since Character.isUpperCase(char) already returns false for non-letter characters.
If you need to convert character to lower-case use Character.toLowerCase(char).
Note, characters are basically integers, so when you write something like this: char c = 65; and print the value, you will see 'A' in output, because 65 is ASCII value for character 'A'. If you add 32, you will obtain 97 which is 'a'. Putting all together you can write something like this:
char c = ...;
// c is in range of upper-case characters
if (c >= 65 && c <= 90) {
char lower = c + 32;
}
// c is in range of lower-case characters
if (c >= 97 && c <= 122) {
char upper = c - 32;
}
Try following method which mutates original list:
protected List<String> enrichValues(List<String> list) {
for (int i = 0; i < list.size(); i++) {
StringBuilder sb = new StringBuilder(list.get(i));
for (int j = 0; j < sb.length(); j++) {
char c = sb.charAt(j);
if ( Character.isUpperCase(c) ) {
sb.insert(++j, Character.toLowerCase(c));
}
}
list.set(i, sb.toString());
}
return list;
}
or this one which creates new list for transformed values:
protected List<String> enrichValues(List<String> original) {
List<String> transformed = new ArrayList<>(list.size());
for (String s : original) {
StringBuilder sb = new StringBuilder(s);
for (int j = 0; j < sb.length(); j++) {
char c = sb.charAt(j);
if ( Character.isUpperCase(c) ) {
sb.insert(++j, Character.toLowerCase(c));
}
}
transformed.add(sb.toString());
}
return transformed;
}
Test:
System.out.println( enrichValues(Arrays.asList("A123", "C123", "15B2")) );
// Output: [Aa123, Cc123, 15Bb2]
To find out whether if a letter is upper case you just need to use the ASCII alphabet. Upper case characters go from 65 to 90.
So you just need to use a loop over the length of your string and check for each char if it´s ASCII value is between 65 and 90. So it would look like this, assuming the variable temp is one character of the string:
if((int)temp >= 65 && (int)temp <= 90){
//Add the lower case character by adding 22 to the character value (lower case are from 97-122)
char additionalChar = ((int)temp)+22;
}
Note that I´ve not tried the code so ((int)temp)+22 might not work that way, but it would look pretty similar.
There is an easier way to do this with regular expressions:
final ArrayList< String > outputList = new ArrayList<>();
for ( String str : list ) {
final String[] letters = str.replaceAll( "[^A-Z]", "" ).toLowerCase().split( "" );
final String result = String.format( str.replaceAll( "([A-Z])", "$1%s" ), letters );
outputList.add( result );
}
return outputList;

Replace only strings that are not substrings of similar strings?

I have a string, 12999986, 31999999, 39949283, 99002999 in which I am trying to replace all of the patterns 99 with the pattern 00. However, the pattern cannot be part of a substring of another similar string, say 999 or 9999. In this example, the output would be 12999986, 31999999, 30049283, 00002999 (39949283 to 30049283, 99002999 to 00002999). I've created this method, but it doesn't really work for larger strings (doesn't find all patterns, inserts strings in random places):
public static String replaceAllExact(String data, String searchString, String replacement) {
List<Integer> locations = new ArrayList<>(); //start (exclusive)
char[] dataChars = data.toCharArray();
char[] searchStringChars = searchString.toCharArray();
char[] replacementChars = replacement.toCharArray();
int i = 0;
int k = 0;
int startIndex = 0;
int searchStringCharsLength = searchStringChars.length - 1;
for(char c : dataChars) {
if(c != searchStringChars[i] && i == 0) { //not the start of a pattern; continue
k++;
continue;
}else if(c == searchStringChars[i] && i == 0) { //might be the pattern we're looking for
startIndex = k;
i++;
}else if((c == searchStringChars[i] && i > searchStringCharsLength) || ((c != searchStringChars[i] && i < searchStringCharsLength) && i != 0)) { //pattern was too long or too short to be the pattern we're looking for
i = 0;
}else if(c == searchStringChars[i] && i < searchStringCharsLength) { //could be the pattern... keep going
i++;
}else if(c != searchStringChars[i] && i != 0 && i == searchStringCharsLength) { //this is the pattern we're looking for
locations.add(startIndex);
i = 0;
}
k++;
}
int offset = 0;
StringBuilder builder = new StringBuilder(data);
for(int l : locations) {
l += offset;
builder.delete(l, l + searchString.length());
builder.insert(l, replacementChars);
offset = (builder.length() - data.length());
}
return builder.toString();
}
How can I accomplish this? Regex solutions would be welcome, if they are possible.
Clarification
A similar string is a string in which a normal replace will replace some of the characters. For example, using the standard library replace(CharSequence target, CharSequence replacement), the string 31999999 would be considered similar because replace(99, 00) can replace some characters.
The string 39349283 is not a similar string because replace(99, 00) cannot replace any characters. The string 39949283 is similar because replace(99, 00) can replace some characters.
If I understand you correctly you want to replace 99 with something else, but only if there is no 9 before or after it.
In that case you can use look-around mechanisms and ensure that
there is no 9 before, via (?<!9)
there is no 9 after, via (?!9)
So you could use str = str.replaceAll("(?<!9)99(?!9)", "00").

Java: Remove non alphabet character from a String without regex

Is there a way to remove all non alphabet character from a String without regex?
I'm trying to check if the String is a palindrome
This is what i tried so far.
public static boolean isPalindrome( String text )
{
int textLength = text.length() - 1;
String reformattedText = text.trim().toLowerCase();
for( int i = 0; i <= textLength; i++ )
{
if( reformattedText.charAt( i ) != reformattedText.charAt( textLength - i ) )
{
return false;
}
}
return true;
}
But if the input is:
System.out.println( isPalindrome( "Are we not pure? No sir! Panama’s moody"
+ "Noriega brags. It is garbage! Irony dooms a man; a prisoner up to new era." ) );
It should be true.
I'm really having a hard time thinking of how to remove or ignore those non alphabet characters on the String.
I would do something like this:
public static String justAlphaChars(String text) {
StringBuilder builder = new StringBuilder();
for (char ch : text.toCharArray())
if (Character.isAlphabetic(ch))
builder.append(ch);
return builder.toString();
}
Just tested method above in your example bellow and worked. Returned true.
System.out.println( isPalindrome( justAlphaChars ( "Are we not pure? No sir! Panama’s moody"
+ "Noriega brags. It is garbage! Irony dooms a man; a prisoner up to new era." ) ) );
OOPS. Java, not Python.
You can still use list-like access in Java, just a bit more work.
char[] letters = text.toCharArray();
int nletters = 0;
for (int i=0; i<letters.length; ++i) {
if (Character.isLetter(letters[i])
letters[nletters++] = Character.toUpperCase(letters[i]);
}
// print out letters in array:
System.out.print("letters only: ");
for (int i=0; i<nletters; ++i) {
System.out.print(letters[i]);
}
System.out.println();
Now use the first nletters positions only in the letters array, since those positions will hold the lowercased letters from the input. An example that just displays the remaining characters is included above.
Now write a loop to compare letters[0] with letters[nletters-1], letters[1] with letters[nletters-2], and so on. If all pairs are equal, you have a palindrome.
String removeNonAlpha(final String word) {
final StringBuilder result = new StringBuilder();
for (final char ch : word.toCharArray()) {
final int ascii = ch;
if (((ascii >= 65) && (ascii <= 90)) || ((ascii >= 97) && (ascii <= 122))) {
result.append(ch);
}
}
return result.toString();
}
Explanation:
The method will retrieve a string containing only A-Z and a-z characters.
I am simply verifying the ascii code for the given char.
Please refer to the ASCII code table

Search for Capital Letter in String

I am trying to search a string for the last index of a capital letter. I don't mind using regular expressions, but I'm not too familiar with them.
int searchPattern = searchString.lastIndexOf(" ");
String resultingString = searchString.substring(searchPattern + 1);
As you can see, with my current code I'm looking for the last space that is included in a string. I need to change this to search for last capital letter.
You can write a method as follows:
public int lastIndexOfUCL(String str) {
for(int i=str.length()-1; i>=0; i--) {
if(Character.isUpperCase(str.charAt(i))) {
return i;
}
}
return -1;
}
Pattern pat = Pattern.compile("[A-Z][^A-Z]*$");
Matcher match = pat.matcher(inputString);
int lastCapitalIndex = -1;
if(match.find())
{
lastCapitalIndex = match.start();
}
lastCapitalIndex will contain the index of the last capital letter in the inputString or -1 if no capitals exist.
EDIT NOTE: Solution formerly contained a loop, now it will work with one call to find() and no looping thanks to an improved regex. Tested new pattern as well, and it worked.
In Android (Java) you can use this:
String s = MyDocumentFileIsHere;
String textWithSpace = s.replaceAll("(.)([A-Z])", "$1 $2");
holder.titleTxt.setText(textWithSpace);
The result of String will be "My Document File Is Here"
You can compare each character of the string with the uppercase characters range in the ASCII table (decimal 65 ('A') to 90 ('Z')).
You can increase the readability of your code and benefit from some other features of modern Java here. Please use the Stream approach for solving this problem.
/**
* Finds the last uppercase letter in a String.
*/
public class FindLastCapitalLetter {
public static void main(String[] args) {
String str = "saveChangesInTheEditor";
int lastUppercaseLetter = findLastUppercaseLetter(str);
if (lastUppercaseLetter != -1) {
System.out.println("The last uppercase letter is "
+ Character.toString((char) lastUppercaseLetter));
} else {
System.out.println("No uppercase letter was found in the String.");
}
}
private static int findLastUppercaseLetter(String str) {
return new StringBuilder(str).reverse().toString().chars()
.filter(c -> Character.isUpperCase(c)).findFirst().orElse(-1);
}
}
Sample output:
The last uppercase letter is E
Also, this code gives you the index of the last capital letter in the String.
import java.util.stream.IntStream;
/**
* Finds the index of the last uppercase letter in a String.
*/
public class FindIndexOfLastUppercaseLetter {
public static void main(String[] args) {
String str = "saveChangesInTheEditor";
int lastUppercaseLetterIndex = findLastUppercaseLetter(str);
if (lastUppercaseLetterIndex != -1) {
System.out.println("The last uppercase letter index is " + lastUppercaseLetterIndex
+ " which is " + str.charAt(lastUppercaseLetterIndex));
} else {
System.out.println("No uppercase letter was found in the String.");
}
}
private static int findLastUppercaseLetter(String str) {
int[] stringChars = str.chars().toArray();
int stringCharsLenght = stringChars.length;
return IntStream.range(0, stringCharsLenght)
.map(i -> stringCharsLenght - i - 1)
.filter(i -> Character.isUpperCase(stringChars[i]))
.findFirst().orElse(-1);
}
}
Sample output:
The last uppercase letter index is 16 which is E
LeetCode - Detect capitals
class Solution {
public boolean detectCapitalUse(String word) {
int len = word.length();
if (word.charAt(0) >= 'A' && word.charAt(0) <= 'Z') {
if (word.charAt(len-1) >= 'A' && word.charAt(len-1) <= 'Z') {
for (int i = 1 ; i < len-1 ; i++) {
if ( word.charAt(i) < 'A' || word.charAt(i) > 'Z')
return false;
}
} else {
for (int i = 1 ; i <= len-1 ; i++) {
if ( word.charAt(i) < 'a' || word.charAt(i) > 'z')
return false;
}
}
} else {
for (int i = 0 ; i <= len-1 ; i++) {
if ( word.charAt(i) < 'a' || word.charAt(i) > 'z')
return false;
}
}
return true;
}
}

Categories