1. Distribution of biscuits

Sort the two arrays first. Then iterate through the cookie array, getting the size of the cookie one at a time, and look for the first index that is less than or equal to the size of the cookie from the corresponding child array.

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        int count = 0;
        int index = 0;
        Arrays.sort(g);
        Arrays.sort(s);
        for (int i = 0; i < s.length; i++) {
            int biscuit = s[i];
            for (int j = index; j < g.length; j++) {
                if (g[j] <= biscuit) {
                    count++;
                    index = j + 1;
                    break; }}}returncount; }}Copy the code
  1. Repeated substring (*)

Use enumeration method. If a string s of length n can be formed by repeating one of its substring s’ of length n’ multiple times, then: N must be a multiple of n’ and for any I ∈[n ‘,n), s[I] = s[i-n’]. Therefore, we can enumerate n’ from small to large and iterate over the string s. Note that a small optimization is that the substring needs to be repeated at least once. So n prime can’t be more than half of n

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        int n = s.length();
        for (int i = 1; i * 2 <= n; ++i) {
            if (n % i == 0) {
                boolean match = true;
                for (int j = i; j < n; ++j) {
                    if(s.charAt(j) ! = s.charAt(j - i)) { match =false;
                        break; }}if (match) {
                    return true; }}}return false; }}Copy the code