Additional resources can be found here.
Describes the asymptotic behavior of a function
can be arbitrarily "loose"
Definition $$ f(n) = O(g(n)) \text{ if there exist constants } c > 0 \text{ and } n_0 > 0 \text{ such that } 0 \leq f(n) \leq c \cdot g(n) \text{ for all } n \geq n_0 $$
O(n²) Better than O(n)?4n² < 1000n whenever n < 250.
n is known to be small and the O(n²) algorithm has a lower constant factor than the O(n) algorithm.O(n²) grows faster with larger n, the actual performance for small n can be better than the O(n) algorithm due to a smaller constant factor.Checking whether an item is present in a linked list: O(n)
n/2 checks)Checking whether an item is present in an unordered binary tree: O(n)
Checking whether an item is present in a binary search tree: O(H)
H = nH = log nO(log n) or faster than O(n) to examine every single item of inputO(n²) for quadratic working memory| Code | Explanation |
|---|---|
char charAt(int index) |
Return the character at the given position. |
int codePointAt(int index) |
Return the Unicode code point at the specified position. |
int length() |
Return the length of the string. |
int codePointCount(int b, int e) |
Return the number of Unicode code points in the range from index b to e. |
| Code | Explanation |
|---|---|
s.contains(CharSequence cs) |
Return True if cs is a subsequence of s. |
s.startsWith(String pre) |
Return True if s starts with the prefix pre. |
s.endsWith(String suf) |
Return True if s ends with the suffix suf. |
int s1.indexOf(int ch) |
Return the index of the first occurrence of ch. |
int s1.indexOf(String s2) |
Return the index of the first occurrence of the string s2. |
int s1.lastIndexOf(ch/s2) |
Return the index of the last occurrence of ch or s2. |
| Code | Explanation |
|---|---|
s.replace(c1, c2) |
Replace all occurrences of c1 with c2. |
s.toLowerCase() |
Convert all characters in s to lowercase. |
s.toUpperCase() |
Convert all characters in s to uppercase. |
| Code | Explanation |
|---|---|
s1.concat(s2)s1 += s2 |
Append s2 to the end of s1. |
String.valueOf(char[] arr) |
Return a new string containing the characters in arr. |
s.substring(int b, int e) |
Return a new string containing the substring from index b to e. |
s.trim() |
Return s without leading and trailing whitespace. |
String[] s.split(String regex) |
Split s around occurrences of the regex pattern. |
| Code | Explanation |
|---|---|
string1 == string2 |
Returns True if string1 and string2 reference the same instance. |
string1.equals(string2) |
Returns True if string1 and string2 have the same content. |
string1.compareTo(string2) |
Returns 0 if equal, -1 if string1 is lexically before string2, +1 if after. |
string1.compareToIgnoreCase(string2) |
Returns 0 if string1 and string2 have the same content, ignoring case. |
s.matches(String regex) |
Returns True if s matches the regular expression regex. |
Character Literal: Matches a specific character
"a": Matches the letter aCharacter Wildcard (.): Matches any single character
".": Matches any characterCharacter Set ([...]): Matches any one of the characters within the brackets
"[aeiou]": Matches any vowel[^...]): Matches anything not in the set[^0-9]: Matches any character that is not a digitRepetition:
X?: Matches 0 or 1 occurrence of X (X is optional)X*: Matches 0 or more occurrences of XX+: Matches 1 or more occurrences of XConcatenation: Combines multiple expressions
XY: Matches X followed by YAlternation (|): Matches either the expression before or after |
X|Y: Matches X or Y^ (Caret): Matches the start of the string
^abc: Matches abc only at the start$ (Dollar): Matches the end of the string
abc$: Matches abc only at the endFiner Control Over Repetition:
X{m,n}: Matches at least m and at most n occurrences of XX{m,}: Matches at least m consecutive occurrences of XX{,n}: Matches at most n consecutive occurrences of XGrouping and Capture:
(X...): Groups expressions to be treated as a single unit...(X)...: Captures matched text for later reference\)Escape Special Characters: Makes a special character literal
\[ : Matches [Referencing Matches:
\1, \2, etc., refer to previously captured groupsShortcut Expressions:
\s: Matches any whitespace\d: Matches any digit ([0-9])\D: Matches any non-digit\w: Matches any word character ([a-zA-Z0-9_])Literal Backslash:
\\ to match a single backslash| Expression | Matches |
|---|---|
abc |
abc only |
ab*c |
ac, abc, abbc, abbbc, abbbbc, etc. |
.\. |
Any character followed by a period |
(a\|bc)d |
acd and bcd |
(a\|(bc))d |
ad and bcd |
x[aeiou]?z |
xz, xaz, xez, xiz, xoz, and xuz |
b{3,4} |
bbb and bbbb |
^a |
a only — multiple leading carets all anchor to start |
^\^a |
^a |
[A-Z] |
A, B, ..., Z |
[^0-9] |
Any character except a digit |
\d{3}-\d{2}-\d{4} |
123-45-6789 |
\bword\b |
word |
X+ |
X, XX, XXX, etc. |
^abc$ |
abc |
foo\|bar |
foo or bar |
a(bc)*d |
ad, abcd, abcbcd, etc. |
\w+@\w+\.\w+ |
example@domain.com |
\s |
A single whitespace character |
(\d{3}) |
Captures three digits (e.g., 123) |
String.split
String.split is a convenience function that internally uses Pattern.compile(re).split(str).String[] parts = "a,b,c".split(",");
Efficient Re-use of Regex
Pattern object.Pattern p = Pattern.compile(regex);
Processing Multiple Matches
Create a Matcher object to find multiple matches within a string.
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(string);
m.matches():
string.matches(regex), checks if the entire string matches the pattern.m.find():
true if found.m.group():
find().Usage Example
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
public static void main(String[] args) {
// Define the regex pattern
String regex = "\\bword\\b"; // "\\b" represents word boundary
// Compile the pattern
Pattern pattern = Pattern.compile(regex);
// Input string to match against
String input = "This is a word in a sentence. Another word appears here.";
// Create a Matcher object
Matcher matcher = pattern.matcher(input);
// Check if the entire string matches the pattern
if (matcher.matches()) {
System.out.println("The entire string matches the pattern.");
} else {
System.out.println("The entire string does not match the pattern.");
}
// Find and print all occurrences of the pattern
while (matcher.find()) {
System.out.println("Found a match: " + matcher.group() + " at position " + matcher.start());
}
}
}
/*
Outputs:
Found a match: word at position 10
Found a match: word at position 38
*/