Force buckle - string

1, String common api

string class

functionexplain
char charAt(int index)Returns the value at the index specified by char.
boolean contains(CharSequence s)Returns true if and only if this string contains the specified sequence of char values.
boolean equals(Object anObject)Compares this string with the specified object
boolean isEmpty()Returns true if and only if length() is 0
int length()Returns the length of this string
String replace(char oldChar, char newChar)Returns a string oldChar resulting from replacing all occurrences in this string newChar
char[] toCharArray()Converts this string to a new character array.
static String valueOf(char c)Returns the string form of the char parameter.
String[] split(String regex)Splits this string into a match for the given regular expression.
static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)Returns a copy of a new String, consisting of CharSequence elements, which is added to the delimiter together with the specified copy.

StringBuffer class
1. The StringBuffer class mainly adds operations such as adding, deleting, modifying and querying strings
2. The StringBuilder class is a subclass of StringBuffer. It is a single thread string cache, which has the advantage of faster speed

functionexplain
StringBuffer append(char c)Append the string representation of the char parameter to this sequence.
StringBuffer insert(int offset, char c)Insert the string representation of the char parameter into this sequence.
StringBuffer reverse()Causes the character sequence to be replaced by the opposite of the sequence.
StringBuffer delete(int start, int end)Delete characters from substrings of this sequence.
int indexOf(String str)Returns the index within the string where the specified substring first appears.

2, Flip the words in the string

Idea 1:
Many languages provide methods such as split, reverse and join for strings, so we can simply call the built-in API to complete the operation:

Use split to divide the string into string arrays according to spaces;
Use reverse to reverse the string array;
Use the join method to piece together an array of strings into a string.
code:

class Solution {
    public String reverseWords(String s) {
        // Remove white space characters at the beginning and end
        s = s.trim();
        // Regular matching uses continuous white space characters as separators
        List<String> wordList = Arrays.asList(s.split("\\s+"));
        Collections.reverse(wordList);
        return String.join(" ", wordList);
    }
}

Idea 2:

1. Remove the beginning, end and middle excess spaces
2. Invert the entire string
3. Reverse each word

class Solution {
   /**
     * 1.Remove excess spaces at the beginning and end and in the middle
     * 2.Inverts the entire string
     * 3.Reverse each word
     */
    public String reverseWords(String s) {
    
        // 1. Remove the beginning, end and middle excess spaces
        StringBuilder sb = removeSpace(s);
        // 2. Invert the entire string
        reverseString(sb, 0, sb.length() - 1);
        // 3. Reverse each word
        reverseEachWord(sb);
        return sb.toString();
    }

    private StringBuilder removeSpace(String s) {
        int start = 0;
        int end = s.length() - 1;
        //First remove the leading and trailing spaces
        while (s.charAt(start) == ' ') start++;
        while (s.charAt(end) == ' ') end--;
        StringBuilder sb = new StringBuilder();
        while (start <= end) {
            char c = s.charAt(start);
            //If the character is not empty, or the last character stored in sb is not empty
            if (c != ' ' || sb.charAt(sb.length() - 1) != ' ') {
                sb.append(c);
            }
            start++;
        }
        return sb;
    }

    /**
     * Inverts the characters of the specified interval [start, end] of the string
     */
    public void reverseString(StringBuilder sb, int start, int end) {
        while (start < end) {
            char temp = sb.charAt(start);
            sb.setCharAt(start, sb.charAt(end));
            sb.setCharAt(end, temp);
            start++;
            end--;
        }
    }

    private void reverseEachWord(StringBuilder sb) {
        int start = 0;
        int end = 1;
        int n = sb.length();
        while (start < n) {
            while (end < n && sb.charAt(end) != ' ') {
                end++;
            }
            reverseString(sb, start, end - 1);
            start = end + 1;
            end = start + 1;
        }
    }
}

Keywords: Algorithm leetcode

Added by metalblend on Wed, 05 Jan 2022 23:35:15 +0200