Move articles out of java-strings part3
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package com.baeldung.matchwords;
|
||||
|
||||
import org.ahocorasick.trie.Emit;
|
||||
import org.ahocorasick.trie.Trie;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class MatchWords {
|
||||
|
||||
public static boolean containsWordsIndexOf(String inputString, String[] words) {
|
||||
boolean found = true;
|
||||
for (String word : words) {
|
||||
if (inputString.indexOf(word) == -1) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
public static boolean containsWords(String inputString, String[] items) {
|
||||
boolean found = true;
|
||||
for (String item : items) {
|
||||
if (!inputString.contains(item)) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
public static boolean containsWordsAhoCorasick(String inputString, String[] words) {
|
||||
Trie trie = Trie.builder()
|
||||
.onlyWholeWords()
|
||||
.addKeywords(words)
|
||||
.build();
|
||||
|
||||
Collection<Emit> emits = trie.parseText(inputString);
|
||||
emits.forEach(System.out::println);
|
||||
|
||||
boolean found = true;
|
||||
for(String word : words) {
|
||||
boolean contains = Arrays.toString(emits.toArray()).contains(word);
|
||||
if (!contains) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
public static boolean containsWordsPatternMatch(String inputString, String[] words) {
|
||||
|
||||
StringBuilder regexp = new StringBuilder();
|
||||
for (String word : words) {
|
||||
regexp.append("(?=.*").append(word).append(")");
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile(regexp.toString());
|
||||
|
||||
return pattern.matcher(inputString).find();
|
||||
}
|
||||
|
||||
public static boolean containsWordsJava8(String inputString, String[] words) {
|
||||
List<String> inputStringList = Arrays.asList(inputString.split(" "));
|
||||
List<String> wordsList = Arrays.asList(words);
|
||||
|
||||
return wordsList.stream().allMatch(inputStringList::contains);
|
||||
}
|
||||
|
||||
public static boolean containsWordsArray(String inputString, String[] words) {
|
||||
List<String> inputStringList = Arrays.asList(inputString.split(" "));
|
||||
List<String> wordsList = Arrays.asList(words);
|
||||
|
||||
return inputStringList.containsAll(wordsList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.palindrom;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Palindrome {
|
||||
|
||||
public boolean isPalindrome(String text) {
|
||||
String clean = text.replaceAll("\\s+", "").toLowerCase();
|
||||
int length = clean.length();
|
||||
int forward = 0;
|
||||
int backward = length - 1;
|
||||
while (backward > forward) {
|
||||
char forwardChar = clean.charAt(forward++);
|
||||
char backwardChar = clean.charAt(backward--);
|
||||
if (forwardChar != backwardChar)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isPalindromeReverseTheString(String text) {
|
||||
StringBuilder reverse = new StringBuilder();
|
||||
String clean = text.replaceAll("\\s+", "").toLowerCase();
|
||||
char[] plain = clean.toCharArray();
|
||||
for (int i = plain.length - 1; i >= 0; i--)
|
||||
reverse.append(plain[i]);
|
||||
return (reverse.toString()).equals(clean);
|
||||
}
|
||||
|
||||
public boolean isPalindromeUsingStringBuilder(String text) {
|
||||
String clean = text.replaceAll("\\s+", "").toLowerCase();
|
||||
StringBuilder plain = new StringBuilder(clean);
|
||||
StringBuilder reverse = plain.reverse();
|
||||
return (reverse.toString()).equals(clean);
|
||||
}
|
||||
|
||||
public boolean isPalindromeUsingStringBuffer(String text) {
|
||||
String clean = text.replaceAll("\\s+", "").toLowerCase();
|
||||
StringBuffer plain = new StringBuffer(clean);
|
||||
StringBuffer reverse = plain.reverse();
|
||||
return (reverse.toString()).equals(clean);
|
||||
}
|
||||
|
||||
public boolean isPalindromeRecursive(String text) {
|
||||
String clean = text.replaceAll("\\s+", "").toLowerCase();
|
||||
return recursivePalindrome(clean, 0, clean.length() - 1);
|
||||
}
|
||||
|
||||
private boolean recursivePalindrome(String text, int forward, int backward) {
|
||||
if (forward == backward)
|
||||
return true;
|
||||
if ((text.charAt(forward)) != (text.charAt(backward)))
|
||||
return false;
|
||||
if (forward < backward + 1) {
|
||||
return recursivePalindrome(text, forward + 1, backward - 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isPalindromeUsingIntStream(String text) {
|
||||
String temp = text.replaceAll("\\s+", "").toLowerCase();
|
||||
return IntStream.range(0, temp.length() / 2)
|
||||
.noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.pangram;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Pangram {
|
||||
private static final int ALPHABET_COUNT = 26;
|
||||
|
||||
public static boolean isPangram(String str) {
|
||||
if (str == null)
|
||||
return false;
|
||||
Boolean[] alphabetMarker = new Boolean[ALPHABET_COUNT];
|
||||
Arrays.fill(alphabetMarker, false);
|
||||
int alphabetIndex = 0;
|
||||
String strUpper = str.toUpperCase();
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
if ('A' <= strUpper.charAt(i) && strUpper.charAt(i) <= 'Z') {
|
||||
alphabetIndex = strUpper.charAt(i) - 'A';
|
||||
alphabetMarker[alphabetIndex] = true;
|
||||
}
|
||||
}
|
||||
for (boolean index : alphabetMarker) {
|
||||
if (!index)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isPangramWithStreams(String str) {
|
||||
if (str == null)
|
||||
return false;
|
||||
|
||||
// filtered character stream
|
||||
String strUpper = str.toUpperCase();
|
||||
Stream<Character> filteredCharStream = strUpper.chars()
|
||||
.filter(item -> ((item >= 'A' && item <= 'Z')))
|
||||
.mapToObj(c -> (char) c);
|
||||
Map<Character, Boolean> alphabetMap = filteredCharStream.collect(Collectors.toMap(item -> item, k -> Boolean.TRUE, (p1, p2) -> p1));
|
||||
|
||||
return (alphabetMap.size() == ALPHABET_COUNT);
|
||||
}
|
||||
|
||||
public static boolean isPerfectPangram(String str) {
|
||||
if (str == null)
|
||||
return false;
|
||||
|
||||
// filtered character stream
|
||||
String strUpper = str.toUpperCase();
|
||||
Stream<Character> filteredCharStream = strUpper.chars()
|
||||
.filter(item -> ((item >= 'A' && item <= 'Z')))
|
||||
.mapToObj(c -> (char) c);
|
||||
Map<Character, Long> alphabetFrequencyMap = filteredCharStream.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
|
||||
|
||||
return (alphabetFrequencyMap.size() == ALPHABET_COUNT && alphabetFrequencyMap.values()
|
||||
.stream()
|
||||
.allMatch(item -> item == 1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.baeldung.removeduplicates;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class RemoveDuplicateFromString {
|
||||
|
||||
|
||||
String removeDuplicatesUsingCharArray(String str) {
|
||||
|
||||
char[] chars = str.toCharArray();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int repeatedCtr;
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
repeatedCtr = 0;
|
||||
for (int j = i + 1; j < chars.length; j++) {
|
||||
if (chars[i] == chars[j]) {
|
||||
repeatedCtr++;
|
||||
}
|
||||
}
|
||||
if (repeatedCtr == 0) {
|
||||
sb.append(chars[i]);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String removeDuplicatesUsinglinkedHashSet(String str) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Set<Character> linkedHashSet = new LinkedHashSet<>();
|
||||
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
linkedHashSet.add(str.charAt(i));
|
||||
}
|
||||
|
||||
for (Character c : linkedHashSet) {
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String removeDuplicatesUsingSorting(String str) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if(!str.isEmpty()) {
|
||||
char[] chars = str.toCharArray();
|
||||
Arrays.sort(chars);
|
||||
|
||||
sb.append(chars[0]);
|
||||
for (int i = 1; i < chars.length; i++) {
|
||||
if (chars[i] != chars[i - 1]) {
|
||||
sb.append(chars[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String removeDuplicatesUsingHashSet(String str) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Set<Character> hashSet = new HashSet<>();
|
||||
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
hashSet.add(str.charAt(i));
|
||||
}
|
||||
|
||||
for (Character c : hashSet) {
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
String removeDuplicatesUsingIndexOf(String str) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int idx;
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
char c = str.charAt(i);
|
||||
idx = str.indexOf(c, i + 1);
|
||||
if (idx == -1) {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
String removeDuplicatesUsingDistinct(String str) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
str.chars().distinct().forEach(c -> sb.append((char) c));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.repetition;
|
||||
|
||||
public class SubstringRepetition {
|
||||
|
||||
public static boolean containsOnlySubstrings(String string) {
|
||||
|
||||
if (string.length() < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
StringBuilder substr = new StringBuilder();
|
||||
for (int i = 0; i < string.length() / 2; i++) {
|
||||
substr.append(string.charAt(i));
|
||||
|
||||
String clearedFromSubstrings = string.replaceAll(substr.toString(), "");
|
||||
|
||||
if (clearedFromSubstrings.length() == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean containsOnlySubstringsEfficient(String string) {
|
||||
|
||||
return ((string + string).indexOf(string, 1) != string.length());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.reverse;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class ReverseStringExamples {
|
||||
|
||||
public static String reverse(String input) {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String output = "";
|
||||
|
||||
for (int i = input.length() - 1; i >= 0; i--) {
|
||||
output = output + input.charAt(i);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public static String reverseUsingStringBuilder(String input) {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder output = new StringBuilder(input).reverse();
|
||||
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
public static String reverseUsingApacheCommons(String input) {
|
||||
return StringUtils.reverse(input);
|
||||
}
|
||||
|
||||
public static String reverseTheOrderOfWords(String sentence) {
|
||||
if (sentence == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
String[] words = sentence.split(" ");
|
||||
|
||||
for (int i = words.length - 1; i >= 0; i--) {
|
||||
output.append(words[i]);
|
||||
output.append(" ");
|
||||
}
|
||||
|
||||
return output.toString()
|
||||
.trim();
|
||||
}
|
||||
|
||||
public static String reverseTheOrderOfWordsUsingApacheCommons(String sentence) {
|
||||
return StringUtils.reverseDelimited(sentence, ' ');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.searching;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WordIndexer {
|
||||
|
||||
public List<Integer> findWord(String textString, String word) {
|
||||
int index = 0;
|
||||
List<Integer> indexes = new ArrayList<Integer>();
|
||||
String lowerCaseTextString = textString.toLowerCase();
|
||||
String lowerCaseWord = word.toLowerCase();
|
||||
|
||||
while(index != -1) {
|
||||
index = lowerCaseTextString.indexOf(lowerCaseWord, index);
|
||||
if (index == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
indexes.add(index);
|
||||
index++;
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<Integer> findWordUpgrade(String textString, String word) {
|
||||
int index = 0;
|
||||
List<Integer> indexes = new ArrayList<Integer>();
|
||||
StringBuilder output = new StringBuilder();
|
||||
String lowerCaseTextString = textString.toLowerCase();
|
||||
String lowerCaseWord = word.toLowerCase();
|
||||
int wordLength = 0;
|
||||
|
||||
while(index != -1){
|
||||
index = lowerCaseTextString.indexOf(lowerCaseWord, index + wordLength); // Slight improvement
|
||||
if (index != -1) {
|
||||
indexes.add(index);
|
||||
}
|
||||
wordLength = word.length();
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.stopwords;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
||||
@Fork(value = 3, warmups = 1)
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
public class RemovingStopwordsPerformanceComparison {
|
||||
|
||||
private String data;
|
||||
|
||||
private List<String> stopwords;
|
||||
|
||||
private String stopwordsRegex;
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Setup
|
||||
public void setup() throws IOException {
|
||||
data = new String(Files.readAllBytes(Paths.get("src/main/resources/shakespeare-hamlet.txt")));
|
||||
data = data.toLowerCase();
|
||||
stopwords = Files.readAllLines(Paths.get("src/main/resources/english_stopwords.txt"));
|
||||
stopwordsRegex = stopwords.stream().collect(Collectors.joining("|", "\\b(", ")\\b\\s?"));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String removeManually() {
|
||||
String[] allWords = data.split(" ");
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for(String word:allWords) {
|
||||
if(! stopwords.contains(word)) {
|
||||
builder.append(word);
|
||||
builder.append(' ');
|
||||
}
|
||||
}
|
||||
return builder.toString().trim();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String removeAll() {
|
||||
ArrayList<String> allWords = Stream.of(data.split(" "))
|
||||
.collect(Collectors.toCollection(ArrayList<String>::new));
|
||||
allWords.removeAll(stopwords);
|
||||
return allWords.stream().collect(Collectors.joining(" "));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String replaceRegex() {
|
||||
return data.replaceAll(stopwordsRegex, "");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
i
|
||||
me
|
||||
my
|
||||
myself
|
||||
we
|
||||
our
|
||||
ours
|
||||
ourselves
|
||||
you
|
||||
your
|
||||
yours
|
||||
yourself
|
||||
yourselves
|
||||
he
|
||||
him
|
||||
his
|
||||
himself
|
||||
she
|
||||
her
|
||||
hers
|
||||
herself
|
||||
it
|
||||
its
|
||||
itself
|
||||
they
|
||||
them
|
||||
their
|
||||
theirs
|
||||
themselves
|
||||
what
|
||||
which
|
||||
who
|
||||
whom
|
||||
this
|
||||
that
|
||||
these
|
||||
those
|
||||
am
|
||||
is
|
||||
are
|
||||
was
|
||||
were
|
||||
be
|
||||
been
|
||||
being
|
||||
have
|
||||
has
|
||||
had
|
||||
having
|
||||
do
|
||||
does
|
||||
did
|
||||
doing
|
||||
a
|
||||
an
|
||||
the
|
||||
and
|
||||
but
|
||||
if
|
||||
or
|
||||
because
|
||||
as
|
||||
until
|
||||
while
|
||||
of
|
||||
at
|
||||
by
|
||||
for
|
||||
with
|
||||
about
|
||||
against
|
||||
between
|
||||
into
|
||||
through
|
||||
during
|
||||
before
|
||||
after
|
||||
above
|
||||
below
|
||||
to
|
||||
from
|
||||
up
|
||||
down
|
||||
in
|
||||
out
|
||||
on
|
||||
off
|
||||
over
|
||||
under
|
||||
again
|
||||
further
|
||||
then
|
||||
once
|
||||
here
|
||||
there
|
||||
when
|
||||
where
|
||||
why
|
||||
how
|
||||
all
|
||||
any
|
||||
both
|
||||
each
|
||||
few
|
||||
more
|
||||
most
|
||||
other
|
||||
some
|
||||
such
|
||||
no
|
||||
nor
|
||||
not
|
||||
only
|
||||
own
|
||||
same
|
||||
so
|
||||
than
|
||||
too
|
||||
very
|
||||
s
|
||||
t
|
||||
can
|
||||
will
|
||||
just
|
||||
don
|
||||
should
|
||||
now
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user