Count the number of consecutive characters

1446. Consecutive characters

Title description:

Given a string s, the "energy" of the string is defined as the length of the longest non-empty substring that contains only one type of character. Please return the energy of the string.

prompt:

  • 1 <= s.length <= 500
  • s contains only lowercase English letters.

Example 1:
Input: s = "leetcode"
Output: 2
Explanation: The length of the substring "ee" is 2 and only contains the character'e'.

Example 2:
Input: s = "abbcccddddeeeeedcba"
Output: 5
Explanation: The substring "eeeee" has a length of 5 and only contains the character'e'.

Example 3:
Input: s = "triplepillooooow"
Output: 5

Example 4:
Input: s = "hooraaaaaaaaaaay"
Output: 11

Example 5:
Input: s = "tourist"
Output: 1

code show as below:

class Solution {
    
    
    public int maxPower(String s) {
    
    
        int n = s.length();
        int ptr = 0;
        int ans = Integer.MIN_VALUE;
        while (ptr < n) {
    
    
            char ch = s.charAt(ptr);
            int count = 0;
            while (ptr < n && s.charAt(ptr) == ch) {
    
    
                ptr++;
                count++;
            }
            ans = Math.max(ans, count);
        }
        return ans;
    }
}

Results of the:
Insert picture description here

830. Location of Larger Groups

Title description:

In a string s composed of lowercase letters, it contains a group of consecutive identical characters.
For example, in the string s = "abbxxxxzyy", there are some groups such as "a", "bb", "xxxx", "z" and "yy". The grouping can be represented by the interval [start, end], where start and end respectively represent the subscripts of the start and end positions of the group. The "xxxx" grouping in the above example is represented by the interval [3,6]. We call all groups that contain more than or equal to three consecutive characters as larger groups. Find each larger grouping interval, sort by the starting position subscript in ascending order, and return the result.

prompt:

  • 1 <= s.length <= 1000
  • s contains only lowercase English letters

Example 1:
Input: s = "abbxxxxzzy"
Output: [[3,6]]
Explanation: "xxxx" is a larger group starting at 3 and ending at 6.

Example 2:
Input: s = "abc"
Output: []
Explanation: "a", "b" and "c" are not large groups that meet the requirements.

Example 3:
Input: s = "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]
Explanation: The larger groups are "ddd", "eeee" and "bbb"

Example 4:
Input: s = "aba"
Output: []

code show as below:

class Solution {
    
    
    public List<List<Integer>> largeGroupPositions(String s) {
    
    
        List<List<Integer>> list = new ArrayList<>();
        int n = s.length();
        int ptr = 0;
        while (ptr < n) {
    
    
            int count = 0;
            char ch = s.charAt(ptr);
            List<Integer> arr = new ArrayList<>();
            while (ptr < n && ch == s.charAt(ptr)) {
    
    
                ptr++;
                count++;
            }
            if (count >= 3) {
    
    
                arr.add(ptr - count);
                arr.add(ptr - 1);
                list.add(arr);
            }
        }
        return list;
    }
}

Results of the:
Insert picture description here

Interview questions 01.06. String compression

Title description:

String compression. Using the number of repeated occurrences of characters, write a method to achieve the basic string compression function. For example, the string aabcccccaaa will become a2b1c5a3. If the "compressed" string does not become shorter, the original string is returned. You can assume that the string contains only uppercase and lowercase English letters (a to z).

Example 1:
Input: "aabcccccaaa"
Output: "a2b1c5a3"

Example 2:
Input: "abbccd"
Output: "abbccd"
Explanation: "abbccd" is compressed into "a1b2c2d1", which is longer than the original string.

code show as below:

class Solution {
    
    
    public String compressString(String s) {
    
    
        int n = s.length();
        int ptr = 0;
        StringBuilder SB = new StringBuilder();
        while (ptr < n) {
    
    
            char ch = s.charAt(ptr);
            int count = 0;
            while (ptr < n && s.charAt(ptr) == ch) {
    
    
                ptr++;
                count++;
            }
            SB.append(ch);
            SB.append(count);
        }
        String str = SB.toString();
        int m = str.length();
        return m >= n ? s : str;
    }
}

Results of the:
Insert picture description here

38. Appearance series

Title description:

Given a positive integer n, output the nth item of the appearance sequence.

"Appearance sequence" is a sequence of integers, starting from the number 1, each item in the sequence is a description of the previous item.

You can think of it as a sequence of numeric strings defined by a recursive formula:

  • countAndSay(1) = “1”
  • countAndSay(n) is a description of countAndSay(n-1), and then converted into another number string.

The first five items are as follows:

  1. 1
  2. 11
  3. 21
  4. 1211
  5. 111221

The first item is the number 1 to
describe the previous item. This number is 1 which means "one 1", which is recorded as "11" to
describe the previous item. This number is 11 which means "two 1s", which is recorded as "21" to
describe the previous one. Item, this number is 21, which means "one 2 + one 1", which is recorded as "1211" to
describe the previous item, this number is 1211, which means "one 1 + one 2 + two 1s", which is recorded as "111221"
to describe one For a numeric string, first divide the string into a minimum number of groups, each group is composed of consecutive at most identical characters. Then for each group, first describe the number of characters, and then describe the characters to form a description group. To convert the description into a numeric string, first replace the number of characters in each group with a number, and then connect all the description groups.

Tip:
1 <= n <= 30

Example 1:
Input: n = 1
Output: "1"
Explanation: This is a basic example.

Example 2:
Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = read "1" = one 1 = "11"
countAndSay(3) = read "11" = two 1 = "21"
countAndSay(4) = read "21" = one 2 + one 1 = "12" + "11" = "1211"

code show as below:

class Solution {
    
    
    public String countAndSay(int n) {
    
    
        String[] str = new String[n];
        str[0] = "1";
        if (n == 1) {
    
    
            return str[0];
        }
        for (int i = 1; i < n; i++) {
    
    
            int len = str[i - 1].length();
            int ptr = 0;
            StringBuilder sb = new StringBuilder();
            while (ptr < len) {
    
    
                char ch = str[i - 1].charAt(ptr);
                int count = 0;
                while (ptr < len && ch == str[i - 1].charAt(ptr)) {
    
    
                    ptr++;
                    count++;
                }
                sb.append(String.valueOf(count));
                sb.append(ch);
            }
            str[i] = sb.toString();
        }
        return str[n - 1];
    }
}

Results of the:
Insert picture description here

443. Compressed String

Title description:

Given a set of characters, use the in-place algorithm to compress them. The compressed length must always be less than or equal to the original array length. Each element of the array should be a character of length 1 (not an int integer type). After modifying the input array in situ, the new length of the array is returned.

Advanced:
Can you solve the problem using only O(1) space?

Tip:
All characters have an ASCII value in the interval [35, 126].
1 <= len(chars) <= 1000.

Example 1:
Input:
["a","a","b","b","c","c","c"]

Output:
return 6, the first 6 characters of the input array should be: ["a","2","b","2","c","3"]

Explanation:
"aa" is replaced by "a2". "Bb" is replaced by "b2". "Ccc" is replaced by "c3".

Example 2:
Input:
["a"]

Output:
return 1, the first character of the input array should be: ["a"]

Explanation:
No string is replaced.

Example 3:
Input:
["a","b","b","b","b","b","b","b","b","b","b", "B","b"]

Output:
return 4, the first 4 characters of the input array should be: ["a", "b", "1", "2"].

Explanation:
Since the character "a" is not repeated, it will not be compressed. "Bbbbbbbbbbbb" is replaced by "b12".
Note that each number has its own position in the array.

code show as below:

class Solution {
    
    
    public int compress(char[] chars) {
    
    
        int n = chars.length;
        int ptr = 0;
        int k = 0;
        while (ptr < n) {
    
    
            char ch = chars[ptr];
            int count = 0;
            while (ptr < n && ch == chars[ptr]) {
    
    
                ptr++;
                count++;
            }
            if (count == 1) {
    
    
                chars[k++] = ch;
            } else {
    
    
                chars[k++] = ch;
                String s = String.valueOf(count);
                int len = s.length();
                for (int i = 0; i < len; i++) {
    
    
                    chars[k++] = s.charAt(i);
                }
            }
        }
        return k;
    }
}

Results of the:
Insert picture description here

925. Long press to enter

Title description:

Your friend is using the keyboard to enter his name. Occasionally, when typing the character c, the key may be long pressed, and the character may be entered one or more times.

You will check the typed characters entered on the keyboard. If it corresponds to your friend's name (some characters may be long-pressed), then it returns True.

prompt:

  • name.length <= 1000
  • typed.length <= 1000
  • The characters of name and typed are both lowercase letters.

Example 1:
Input: name = "alex", typed = "aaleex"
Output: true
Explanation: The'a' and'e' in'alex ' are long pressed.

Example 2:
Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation:'e' must be typed twice, but this is not the case in the output of typed.

Example 3:
Input: name = "leelee", typed = "lleeelee"
Output: true

Example 4:
Input: name = "laiden", typed = "laiden"
Output: true
Explanation: Long-pressing the characters in the name is not necessary.

code show as below:

class Solution {
    
    
    public boolean isLongPressedName(String name, String typed) {
    
    
        int m = name.length();
        int n = typed.length();
        int ptr1 = 0;
        int ptr2 = 0;
        while (ptr1 < m && ptr2 < n) {
    
    
            int count1 = 0;
            int count2 = 0;
            char ch1 = name.charAt(ptr1);
            char ch2 = typed.charAt(ptr2);
            while (ptr1 < m && ch1 == name.charAt(ptr1)) {
    
    
                ptr1++;
                count1++;
            }
            while (ptr2 < n && ch2 == typed.charAt(ptr2)) {
    
    
                ptr2++;
                count2++;
            }
            if (ch1 != ch2 || count1 > count2) {
    
    
                return false;
            }
        }
        return ptr1 == m && ptr2 == n;
    }
}

Results of the:
Insert picture description here

Guess you like

Origin blog.csdn.net/FYPPPP/article/details/114489722