Check if string contains all strings from array - java

How to check if String contains all Strings from Array.
My current code:
String word = "abc";
String[] keywords = {"a", "d"};
for(int i = 0; i < keywords.length; i++){
if(word.contains(keywords[i])){
System.out.println("Yes");
}else{
System.out.println("No");
}
}

The code would look much more nicer if you wrap it into a separate method:
public static boolean containsAllWords(String word, String ...keywords) {
for (String k : keywords)
if (!word.contains(k)) return false;
return true;
}

If you are using Java 8+, you could use a Stream and test if all of the elements match your criteria with one line. Like,
if (Stream.of(keywords).allMatch(word::contains)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
In earlier versions, or if you want to understand what the above is doing, it might look something like
boolean allMatch = true;
for (String kw : keywords) { // <-- for each kw in keywords
if (!word.contains(kw)) { // <-- if "word" doesn't contain kw
allMatch = false; // <-- set allMatch to false
break; // <-- stop checking
}
}
if (allMatch) {
System.out.println("Yes");
} else {
System.out.println("No");
}

Use a boolean variable that will tell you if every keyword is matched. Set it to true as default value. Then check every keword: if any one is not contained in your word, stop searching and set your variable to false.
boolean containsAll = true;
for (String keyword : keywords){
if (!word.contains(keyword)){
containsAll = false;
break;
}
}

Note this solution work only if your keywords contain one char, there are many answers already mentioned if your keywords contain more then one char.
With one line :
boolean contain = Arrays.asList(word.split("")).containsAll(Arrays.asList(keywords));
The idea is :
String word = "abc";
String[] split = word.split("");
Split your String to get an array of chars split = {a, b, c}
String[] keywords = {"a", "b"};
Check if your array1 contain all the element of the second array2 using containsAll
boolean contain = Arrays.asList(split).containsAll(Arrays.asList(keywords));

Either use StringUtils (org.apache.commons.lang.StringUtils). It will return the index of the first occurrence of keywords or -1 if it is not there.
StringUtils.indexOfAny(word, keywords);
Or you can use Arrays which will return the boolean value.
Arrays.asList(word).contains(keywords)

A better code would be:
for(String s: keywords){
if(word.contains(s)){
System.out.println("Yes");
}else{
System.out.println("No");
}
}

Simply use a counter:
String word = "abc";
String[] keywords = {"a", "d"};
int counter = 0;
for(int i = 0; i < keywords.length; i++){
if(word.contains(keywords[i])){
counter++;
}
}
if(counter == keywords.length){
System.out.println("Yes");
}else{
System.out.println("No");
}
If the counter equals the keywords length, it means that all elements are contained. With this solution you will also be able to find out how many keywords are matched by the word.

Related

How to compare 2 strings if they have spaces?

I'm having some problem with a simple program I'm working on, I just need to ask the user if he wants the regular price or the sale price. And he needs to respond in a string value. Now I know that I just have to use if(price.equals("sale") == true). But the problem comes if I want the user to type something that has spaces in the words, example: if(price.equals("regular price") == true) when I the user types in "regular price" I don't get a response I wanted.
I'm also learning Java by the way.
You can determine if a string contains a word like this:
if (price.toLowerCase().contains("regular"))
If you want to get fancy, you can use regex to match a whole word (eg not "irregular"):
if (price.matches("(?i).*\\bregular\\b.*"))
java equals() method/function works perfectly with the spaces too. Also you don't need to compare the result with true/false. If two string is equal then equals() will automatically return true else it will return false.
public class MyClass {
public static void main(String args[]) {
String a = "A B C";
if(a.equals("A B C")){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
I tested the following program and it returned true while the string has spaces in it.
You want to compare strings with spaces right?
Then you can first compare their lengths, then
If string contains space split it by it, again compare lengths, next check every string.
If it not contains space, just use equals once, here what i come up with:
class cmp {
public static void main(String[] args) {
String s = "test some";
String s2 = "test some";
String s3 = "test not";
String s4 = "testxsome";
System.out.println(cmpStr(s,s2)); // True
System.out.println(cmpStr(s,s3)); // False
System.out.println(cmpStr(s,s4)); // False
}
public static boolean cmpStr(String str1, String str2) {
if (str1.length() != str2.length()) {
System.out.println("Length mismatch.");
return false;
}
if (str1.contains(" ") || str2.contains(" ")) {
String[] a1 = str1.split(" ");
String[] a2 = str2.split(" ");
if (a1.length != a2.length) {
System.out.println("Split Length mismatch.");
return false;
}
for (int i = 0; i < a1.length; i++) {
if (!a1[i].equals(a2[i])) {
System.out.println("One of split mismatch." + a1[i] + " " + a2[i] );
return false;
}
}
} else {
if (!str1.equals(str2)) {
System.out.println("Regular equals returns false.");
return false;
}
}
return true;
}
}
Here I see three methods
Remove all the spaces and hidden characters from the string and check
String input = "regular price";
input = input.replaceAll("\\s+","");
if(price.equals(input))
{
//your code here
}
Check whether the word contains keywords
if (price.toLowerCase().contains("regular"))
Create a regular expression to match your words

How to locate simple words amongst compound/simple words using Java?

I have a list of words that have both 'simple' and 'compound' words in them, and would like to implement an algorithm that prints out a list of words without the compound words that are made up of the simple words.
Sampel input:
chat, ever, snapchat, snap, salesperson, per, person, sales, son, whatsoever, what, so
Desired output:
chat, ever, snap, per, sales, son, what, so
I have written the following, but am stuck as to how to take it on from here:
private static String[] find(String[] words) {
ArrayList<String> alist = new ArrayList<String>();
Set<String> r1 = new HashSet<String>();
for(String s: words){
alist.add(s);
}
Collections.sort(alist,new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.length()-o2.length();
}
});
int count= 0;
for(int i=0;i<alist.size();i++){
String check = alist.get(i);
r1.add(check);
for(int j=i+1;j<alist.size();j++){
String temp = alist.get(j);
//System.out.println(check+" "+temp);
if(temp.contains(check) ){
alist.remove(temp);
}
}
}
System.out.println(r1.toString());
String res[] = new String[r1.size()];
for(String i:words){
if(r1.contains(i)){
res[count++] = i;
}
}
return res;
}
Any guidance/insight or suggestions to a better approach would be appreciated.
I tried to go through your code, looks like "son" is not in your output. I believe it failed because of this line:
if(temp.contains(check)) { <-- wrong check.
alist.remove(temp);
}
So instead of simply checking if temp.contains(check), you should have a small loop that does the following:
does temp start with check?
if 1) passed, then let temp = temp.substring(check.length), then go back to 1) again, until temp == "";
Another implementation would be setting up a trie (https://en.wikipedia.org/wiki/Trie) and check using that?
sort the word list based on word length
foreach of the word, if the word is not in the trie, add it to the trie. otherwise, this is either a dup or a compound word
output the trie into a list of words using DFS.
step 1 make sure that when u check for a compound word, its simple word is already in the trie.
I didn't try to find the bug in your code, but rather wrote my own impl using a simple loop and a recursive helper method:
private static String[] find(String[] array) {
Set<String> words = new LinkedHashSet<>(Arrays.asList(array));
Set<String> otherWords = new HashSet<>(words);
for (Iterator<String> i = words.iterator(); i.hasNext(); ) {
String next = i.next();
otherWords.remove(next);
if (isCompound(next, otherWords)) {
i.remove();
} else {
otherWords.add(next);
}
}
return words.stream().toArray(String[]::new);
}
private static boolean isCompound(String string, Set<String> otherWords) {
if (otherWords.contains(string)) {
return true;
}
for (String word : otherWords) {
if (string.startsWith(word)) {
return isCompound(string.replaceAll("^" + word, ""), otherWords);
}
if (string.endsWith(word)) {
return isCompound(string.replaceAll(word + "$", ""), otherWords);
}
}
return false;
}
See live demo.
This produces your desired output, which requires preserving word order.
Explanation
A compound word is comprised solely of other words in the list. Importantly, this implies that compound words both start and end with other words. Rather than search for other words at every position in a word, we can use this fact to only check the start/end , which greatly simplifies the code.
Thus: for each word in the list, if it start/ends with another word, remove that word and repeat the process until there's nothing left, at which point you know the word is compound.
A set of "other words", which is the full set with the current word removed, is passed to the helper method to further simplify the code.
Here is my straightforward n^2 solution:
static String[] simpleWords(String[] words) {
String[] result;
HashSet<Integer> map = new HashSet<>();
for(int i = 0; i < words.length; i++) {
String word = words[i];
for(int j = 0; j < words.length; j++) {
if(j != i) {
word = word.replaceAll(words[j], "");
}
}
if(!word.equals("")) {
map.add(i);
}
}
result = new String[map.size()];
int i = 0;
for(int index: map) {
result[i] = words[index];
i++;
}
return result;
}

Java how to compare unordered string arrays [duplicate]

This question already has answers here:
how to compare two string arrays without java utils
(3 answers)
Closed 7 years ago.
I have to arrays of string
Array 1
Dog
Cat
Mouse
Chicken
Array 2
Cat
Dog
Mouse
Chicken
How can I check if the arrays comtains the same elements (order does not matter)
I guess I should first sort the array and than to compare
I am looking for a boolean answer
EDIT using Java utils is an option for me, I am just not familiar with JAVA enough
Just sort them both and iterate over the elements to compare them all:
public boolean compareStringArrays(String[] arr1, String[] arr2) {
if (arr1.length != arr2.length)
return false;
String[] arr1Copy = arr1.clone();
String[] arr2Copy = arr2.clone();
Arrays.sort(arr1Copy);
Arrays.sort(arr2Copy);
for (int i=0; i<arr1Copy.length; i++) {
if (!arr1Copy[i].equals(arr2Copy[i]))
return false;
}
return true;
}
Note that I make copies of the arrays here: this is so the original order of the arrays passed in is preserved. There's also an optimisation to check the lengths are the same first, as if one array has more elements than the other they are obviously not equal.
EDIT
you can also use Arrays.equals() instead of a for loop (which I originally didn't think of but seems obvious now), so you could achieve this with a one-liner:
Arrays.equals(Arrays.sort(arr1.clone()), Arrays.sort(arr2.clone()));
ArrayList<String> arrList1 = new ArrayList<>(Arrays.asList(arr1));
ArrayList<String> arrList2 = new ArrayList<>(Arrays.asList(arr2));
Collections.sort(arrList1);
Collections.sort(arrList2);
if (Arrays.equals(arrList1.toArray(), arrList2.toArray())) {
//They have exactly the same elements
}
EDIT:
Old answer:
ArrayList<String> arrList1 = new ArrayList<>(Arrays.asList(arr1));
ArrayList<String> arrList2 = new ArrayList<>(Arrays.asList(arr2));
if (arrList1.containsAll(arrList2) && arrList2.containsAll(arrList1)) {
//They have the same elements, not necessarily the same number
}
The top answer will tell you if they both contain the same elements, as well as if they have the same number, Bottom answer will tell you if they both have the same elements, but doesn't tell you if any elements are duplicated
EDIT again:
Firstly I posted:
if (arrList1.containsAll(arrList2) && arrList2.containsAll(arrList1)
&& arrList1.size() == arrList2.size())
Checking the size is equal is redundant, since if we have the lists:
Cat
Cat
Dog
and
Cat
Dog
Dog
The expression would evaluate to true, but they do not have exactly the same elements
Here is the method:
public boolean compareArray(){
boolean isSameArray=false;
String[] arr1={"Dog","Cat","Mouse","Chicken"};
String[] arr2={"Cat","Dog","Mouse","Chicken"};
Arrays.sort(arr1);
Arrays.sort(arr2);
if(Arrays.equals(arr1, arr2)){
isSameArray=true;
}else{
isSameArray=false;
}
return isSameArray;
}
That is very easy. Just do like this:
ArrayList<String> firstArray=new ArrayList<>();
ArrayList<String> secondArray=new ArrayList<>();
firstArray.add("Dog");
firstArray.add("Cat");
firstArray.add("Mouse");
firstArray.add("Chicken");
secondArray.add("Cat");
secondArray.add("Dog");
secondArray.add("Mouse");
secondArray.add("Chicken");
boolean areEqual=firstArray.containsAll(secondArray);
if(areEqual)
System.out.println("Voila!");
else
System.out.println("Oppps!");
May ways how to do that
- probably the best way is to make sort and compare Arrays like Collections(Arrays.sort(arrayToSort)), objects (Arrays.equals(arr1,arr2))
- another way is just iterate over 1st and 2nd array and try to find items one by one (bad idea, not such effective, but good to understand and explain)- something like following:
String[] arr1={"Dog","Cat","Mouse","Chicken"};
//String[] arr2={"Dog","Mouse","Chicken"}; //FALSE
String[] arr2={"Cat","Dog","Mouse","Chicken"}; //TRUE
boolean foundAll = true;
if(arr1.length != arr2.length){
foundAll = false;
}else{
for (int i = 0; i < arr1.length; i++) {
boolean foundActual = false;
for (int j = 0; j < arr2.length; j++) {
if(arr1[i].equals(arr2[j])){
foundActual = true;
break;
}
System.out.println("arr1 elem: " + arr1[i] + " arr2 elem: "+ arr2[j]);
}
System.out.println("\n");
if(!foundActual){
foundAll = false;
break;
}
}
}
System.out.println("found all: " + foundAll);
}
String arr1[] = {//your content goes here};
String arr2[] = {//your content goes here};
Arrays.sort(arr1);
Arrays.sort(arr2);
if(arr1.length != arr2.length){
retrn false;
}else{
boolean isSimilar = true;
for(int i=0; i< arr1.length; i++){
if(!(arr1[i].equals(arr2[i]))){
isSimilar = false;
}
}
}
return isSimilar;

Find if a string has all unique chars using recursion

Just brushing up on some simple java stuff. I am trying to check if a string is unique, and I figured the best way would be to do so through recursion. Here is what I have so far, but am getting an out of bounds error, obviously i'm overlooking something pretty simple:
public class uniqueCharString {
public static void main(String [] args){
String a = "abcdefghijk";
System.out.println(unique(a));
}
public static boolean unique(String s){
if(s.substring(1).contains(String.valueOf(s.charAt(0)))){
return false;
}
else return unique(s.substring(1));
}
}
okay so I finished my way of thinking. I got some good advice from you guys, but I wanted to finish my thought process. How does this solution compare to some of the ones where you guys said use a set?
public static boolean unique(String s){
for(int x = 0; x < s.length(); x++){
if(s.substring(x+1).contains(String.valueOf(s.charAt(x)))){
return false;
}
}
return true;
}
The most efficient way to do this is with a Set. Iterate over each character and add them to a set. If the add operation returns false, then the character is already in your set and the String has not-all-unique chars, otherwise all are unique
String s = "some string blah blah blah";
Set<Character> set = new HashSet<>();
for (char c : s.toCharArray())
{
boolean elementFirstAdded = set.add(c);
if (!elementFirstAdded)
//Duplicate
}
//Not duplicate
Like #Kon said, a Set is (probably) more efficient.
However, to use recursion you need to add a termination condition: your function never returns true!
A zero or one length string must be unique (well, unique zero-length is a bit ambiguous...): add this at the top:
if (s.length() <= 1) {
return true;
}
Another way of doing this, in case you're crazy about performance-tuning homework problems :)
public static boolean unique(String s) {
BitSet chars = new BitSet(Character.SIZE);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (chars.get(c)) {
return false;
}
chars.set(c);
}
return true;
}
If you don't need to know what was duplicated I would just throw everything into the Set(), because a Set() cannot contain duplicates. They will never be added to the Set(). And if you just want to know if you had duplicates you can check the length of the String before and then the size of the HashSet() after.
Like this:
String a = "abcdefghijk";
String[] ary = a.split("");
Set<T> mySet = new HashSet<T>(Arrays.asList(words))
if (mySet.size() != ary.length){
//do something
}

In Java, how can I test if an Array contains the same value?

I'm looking for a method to detect if all objects within an array(list) are the same.
e. g:
arraylist1 = {"1", "1", "1", "1"} // elements are the same
arraylist2 = {"1", "1", "0", "1"} // elements are not the same
Thanks for help
Java 8 solution :
boolean match = Arrays.stream(arr).allMatch(s -> s.equals(arr[0]));
Same logic for lists :
boolean match = list.stream().allMatch(s -> s.equals(list.get(0)));
It comes quite complicated if there are only or any null values in the array (resulting in a NullPointerException). So possible workarounds are:
Using Predicate.isEqual, it uses the static method equals from the Objects class and it will do the null check for you before calling equals on the first argument.
boolean match = Arrays.stream(arr).allMatch(Predicate.isEqual(arr[0]));
boolean match = Arrays.stream(arr).allMatch(s -> Objects.equals(arr[0], s));
Using distinct() and count() :
boolean match = Arrays.stream(arr).distinct().count() == 1;
that can be improved into Arrays.stream(arr).distinct().limit(2).count() == 1; as there is no need to check the all pipeline's content if you already find 2 distinct elements.
public static boolean AreAllSame(String[] array)
{
boolean isFirstElementNull = array[0] == null;
for(int i = 1; i < array.length; i++)
{
if(isFirstElementNull)
if(array[i] != null) return false;
else
if(!array[0].equals(array[i])) return false;
}
return true;
}
Please feel free to correct any syntax mistakes. I fear my Java-fu may be lacking today.
if( new HashSet<String>(Arrays.asList(yourArray)).size() == 1 ){
// All the elements are the same
}
If your list is empty return true.
If not, loop through it and check if all elements are equal to the element at index 0.
If so, return true, otherwise return false.
public boolean containsSameValues(int[] array) {
if(array.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
int first = array[0];
for(int i=0;i<array.length;i++) {
if(array[i] != first) {
return false;
}
}
return true;
}
boolean containsAllValues=false;
boolean t=false;
int isto=0;
for(int k=0;k<arraylist1.size();k++){
for(int n=0;n<arraylist2.size();n++){
if(arraylist1.get(k).equals(arraylist2.get(n))){
t=true;
}
}
if(t){
isto++;
}else{
isto=0;
break;
}
}
if(isto!=0){
containsAllValues=true;
}
With this you can check if arraylist2 contains values from arraylist1.
You can sort the array and then use the Array equals method:
public boolean equalArrays(int []f ,int [] g){
Arrays.sort(f);
Arrays.sort(g);
if (Arrays.equals(f, g))
return true;
return false;
}
To test it out:
int [] h={1,1,0,1};
int [] t={1,1,1,0};
System.out.println(cc.equalArrays(h,t));
For Java 8, Alexis C's solution is probably the best. However, if you're stuck with <= Java 7 and don't think David Wallace's approach is expressive enough, you could also try this:
boolean result = Collections.frequency(yourList, "1") == yourList.size();
Basically what this does is that it checks whether the number of elements in your list equal to "1" matches the total number of elements in the list. Pretty straightforward.
By the way -- this is pure Java Collections API, and I believe Collection.frequency(..) has been there since at least JDK 1.5. See the javadocs for more information.
EDIT: Here's a quick fiddle if you want to take this for a test drive.
The easiest one:
public static boolean checkTheSame(int[] numbers) {
for (int i = 0; i < numbers.length - 1; i++) {
if (numbers[i] != numbers[i + 1]) {
return false;
}
}
return true;
}

Categories

Resources