String

744. Find Smallest Letter Greater Than Target

public int firstUniqChar(String s) {
    int[] count = new int[26];
    int[] firstIndices = new int[26];
    Arrays.fill(firstIndices, - 1);
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        count[c - 'a']++;
        if (firstIndices[c - 'a'] == -1) {
            firstIndices[c - 'a'] = i;
        }
    }
    int res = Integer.MAX_VALUE;
    for (char c = 'a'; c <= 'z'; c++) {
        if (firstIndices[c - 'a'] != -1 && count[c - 'a'] == 1) {
            res = Math.min(res, firstIndices[c - 'a']);
        }
    }
    return res == Integer.MAX_VALUE ? -1 : res;
}

Use a HashMap to record the order, verify the order and length.

Add string from right to left, if there one string is out of digits, count as zero.

Use StringBuilder to build characters when it is a letter. When encounter non-letter, build string and detect if it is not in banned and is the most common word.

T: O(n) S: O(n)

Convert each String to a map, get min count of all characters between 'a' and 'z'. Add the character to result.

796. Rotate String

Expand around the center, record the L and R when the length of palindromic substring is longer than R - L. return s.substring(L, R + 1).

Sort and loop through. For example, "/a","/a/b","/c/d" you can remove "/a/b","/c/d" because they starts with "/a". T:O(nlogn)

A more elegant version:

Trie. T: O(n * k) where k is average string length.

See section.

Swap x and y to make strings equal, there are three cases:

  1. Case 1: xx, yy => minimum swap is 1

  2. Case 2: xy, yx => minimum swap is 2

  3. Case 3: xy, xx => not possible (have odd number of xy and yx)

case1 is calculated by xy / 2 + yx / 2, case 2 is calculated by xy % 2 + yx % 2

It is a graph of mapping char vs char. Use a hashmap to store this relationship. When building the graph, we also have to validate if theres is any mismatch between the existing node. If yes, return false;

The cycle is detected when the number of relationships = 26. Which means there is a cycle of 26 lowercase characters. This is another false case.

S = "abcd", indexes = [0,2], sources = ["a","cd"], targets = ["eee","ffff"].

Use a map of Index, String[]{source, target} to store the relationship.

Use a hashmap to check the order between two characters. The total number of edges can't be larger and equal to 26 since it doesn't allow cycle in the graph.

DFS to traverse and append strings, check if the string combo is unique.

T: O(2^N)

Use two hashmaps to check the character relation.

Save all substrings with the same characters and its length to a list. Detect if the current and it is + 2 position substring has the same character, and their in between substring has 1 length.

The idea is to keep track of all repeated character segments and see if the neighbor segments (segments separated by one other character) can be merged:

  1. There exist a third segment with same character, swap a same character from a third segment. Merged length = len(segment1) + 1 + len(segment2)

  2. There does not exist a third segment with same character, swap a character from the first character of the first segment, or swapping the last character from the second segment, to the separating index. Merged length = len(segment1) + len(segment2)

Otherwise, if there are multiple segments of a character but they are not neighbor, we can only swap a character from one to the other. New length = len(segment) + 1

Determine if word is typo because of stuck key

Given a dictionary of valid words, write a function isTypoBecauseStuckKey() that accepts a string to determine if the string has a typo that is strictly caused by a stuck key.

Example:

Input: Dictionary: { hello, cat, world, dog, bird, grass, green, help, greet, great }

String: bbbirrrdddd

Output: True

Explanation: The character's 'b', 'r', & 'd' all repeat. Assuming their keys got stuck, we can form the word 'bird', which exists in the dictionary.

Example:

Input:

Dictionary: { hello, cat, world, dog, bird, grass, green, help, greet, great }

String: gggraasssa

Output: False

Explanation: The a at the end is not the result of a stuck key, and thus it is impossible for it to exist in the dictionary.

Trie

3481. Apply Substitutions

30. Substring with Concatenation of All Words

443. String Compression

2099. Find Subsequence of Length K With the Largest Sum

3597. Partition String

Last updated

Was this helpful?