what's the fastest way to check if specific letter/number appear in string (and how many times)? For example:
I let user write something and got "Test020301423" (ofc I store that data in string variable). Now I want to count how many times "0" appears in string. What's the fastest/best way to do that?
I assume you want to search for different characters in the string to check the availability and occurrences. If so, you can do the following:
Build a Map<Character, Integer> where you will store each character of the string as key and the occurrences as values. To build this map, you need to iterate through every character of the string. So it will take O(n) times to build the map.
Now for every search, you can just check if any specific character is in the map and if it exists then see the value (occurrences). It will take O(1) for every search.
Related
I am trying to find whether a part of given string A can be or can not be rearranged to given string B (Boolean output).
Since the algorithm must be at most O(n), to ease it, I used stringA.retainAll(stringB), so now I know string A and string B consist of the same set of characters and now the whole task smells like regex.
And .. reading about regex, I might be now having two problems(c).
The question is, do I potentially face a risk of getting O(infinity) by using regex or its more efficient to use StreamAPI with the purpose of finding whether each character of string A has enough duplicates to cover each of character of string B? Let alone regex syntax is not easy to read and build.
As of now, I can't use sorting (any sorting is at least n*log(n)) nor hashsets and the likes (as it eliminates duplicates in both strings).
Thank you.
You can use a HashMap<Character,Integer> to count the number of occurrences of each character of the first String. That would take linear time.
Then, for each Character of the second String, find if it's in the HashMap and decrement the counter (if it's still positive). This will also take linear time, and if you manage to decrement the counters for all the characters of the second String, you succeed.
I use a hashset for a dictionary. Now I would like to filter out words that do not start with my substring. So it should be something like this:
String word = 'ab';
List<String> list = Arrays.asList(word);
boolean result = lexiconSet.retainAll(list);
And instead of this resulting in the lexicon only containing the word 'ab', I would like to keep all words beginning with 'ab'. How can I do this?
I know I can convert the set to a string arraylist, and loop over all elements to see if the strings starts with 'ab', but since I think this can be time consuming and not efficient, I would like to hear better solutions. Thank you in advance!
With Java 8, life is easy:
list.removeIf(s -> !s.startsWith("ab"));
This will remove all elements that don't begin with "ab".
Note that you can use values() to retrieve the map's values and work directly on them, without the need to convert to ArrayList.
My question is very similar to this one
Java: method to get position of a match in a String?
Except that I will be searching through very long strings (hundreds of megabytes) so I would like for the method to give up after a certain index.
Something like this except with an end index as well. Is there a library that provides this functionality?
Ye you can co this in 2 steps.
Get the Substring in which you want to search i.e. from beginning to the endIndex(or Certain index) using subString(0, certainIndex)
Then use indexOf(matchString) to find the location i=of the first occurence of the pattern into the String.
What is an efficient way to find out all the unique words between 2 sentences in java and store them? What data structure should be used to store the words?
Store words from the first sentence in hashset and then iterate over ords in second sentence to see if its already there in hashset
Put all words from one sentence in a set, then pass through words of the second sentence. If the word exists in a set, take it out of the set, otherwise put it into the set.
A simple way of achieving this is:
//I use regular expression to remove punctuation marks
//II use split to convert the sentences into collections of "words"
//III create a variable that is an implementation of java.util.set (to store unique words)
//III iterate over the collections
// add words from each sentence to the set variable (that way the word will only be stored once)
Hope this helps
I have 100 words. All 100 words are look like this.
EnglishWord,EngMeaning,NumberofW… meaning,31
In that I want to retrieve EnglishWord, e.g. Friendship alone for 100 words by using Java program.
I am assuming you have a "body" (main string), containing a list of substrings and you want to retrieve any specific one substring from within.
This looks a lot like homework/exercise, so I'll avoid giving you a ready-to-roll answer, since you need to achieve a solution yourself for it to be of any value, but the general steps you will need are the following:
1:
Be able to separate each substring (entry) from the others (the base string) in an organized fashion.
This can be done (for the string case), as #kylc said, with String's split function, which uses a REGEX (PATTERN) to define divisors (one or more), that then is/are used to divide the string into an array of multiple substrings.
String[] arrayOfEntries /*something to hold the result*/ = yourStringVar.split("," /*your split regex pattern*/);
NOTE: For more information on these, here are the links: String's split function, Pattern.
2:
Be able to acquire any specific entry withing an array of entries.
This is best done with a function you can reuse for other works. You need to define a "target" (what/which is going to be acquired) and a "source" (group of entries to acquire "target" from).
All you have to do is loop the "source", and for each entry there, compare to "target" for a match; When a match is found, just return it.
That's it! The rest is up to you!