count-the-repetitions

https://leetcode.com/problems/count-the-repetitions/

下面是我的方法,结果对的,超时了。。。

复制代码
package com.company;


class Solution {
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
        int len1 = s1.length();
        int[][]stores = new int[26][len1];
        int[] pos = new int[26];
        int[] cur = new int[26];
        int index;

        for (int i=0; i<len1; i++) {
            index = s1.charAt(i)-'a';
            stores[index][pos[index]] = i;
            pos[index] = pos[index] + 1;
        }

        int curPos = 0;
        int ret = 0;
        int len2 = s2.length();
        while (true) {

            for (int i=0; i<n2; i++) {
                for (int j=0; j<len2; j++) {
                    index = s2.charAt(j) - 'a';

                    if (cur[index] >= pos[index] * n1) {

                        return ret;
                    }

                    int newPos = 0;
                    do {
                        newPos = cur[index] / pos[index] * len1 + stores[index][cur[index] % pos[index]];
                        cur[index] = cur[index] + 1;
                    } while (newPos < curPos && cur[index] < pos[index] * n1);

                    if (newPos < curPos) {
                        return ret;
                    }
                    curPos = newPos + 1;


                }
            }
            ret++;

        }

    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        String s1 = "niconiconi";
        int n1 = 99981;
        String s2 = "nico";
        int n2 = 81;

        Solution solution = new Solution();
        int ret  = solution.getMaxRepetitions(s1, n1, s2, n2);

        // Your Codec object will be instantiated and called as such:
        System.out.printf("ret:%d\n", ret);

        System.out.println();

    }

}
复制代码

 

优化之后的结果,还是超时:

加了string到array的优化,另外每次循环之后坐个判断剪枝。

 

复制代码
package com.company;


class Solution {
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
        int len1 = s1.length();
        int[][]stores = new int[26][len1];
        int[] pos = new int[26];
        int[] cur = new int[26];
        int index;

        for (int i=0; i<len1; i++) {
            index = s1.charAt(i)-'a';
            stores[index][pos[index]] = i;
            pos[index] = pos[index] + 1;
        }

        int curPos = 0;
        int ret = 0;
        int len2 = s2.length();
        char[] array2 = s2.toCharArray();
        while (true) {

            for (int i=0; i<n2; i++) {
                for (int j=0; j<len2; j++) {
                    index = array2[j] - 'a';

                    int newPos = 0;
                    while (cur[index] < pos[index] * n1) {
                        newPos = cur[index] / pos[index] * len1 + stores[index][cur[index] % pos[index]];
                        cur[index] = cur[index] + 1;
                        if (newPos >= curPos) {
                            break;
                        }
                    }

                    if (newPos < curPos) {
                        /*System.out.printf("index %d cur[index] %d pos[index] %d cur/-pos %d, store %d\n",
                                index, cur[index], pos[index], cur[index] % pos[index], stores[index][cur[index] % pos[index]]);

                        System.out.printf("newPos %d curPos %d\n",
                                newPos, curPos);
                                */
                        return ret;
                    }
                    curPos = newPos + 1;


                }
            }
            ret++;
            for (int i=0; i<26; i++) {
                if (pos[i] > 0 && cur[i] >= pos[i] * n1) {
                    return ret;
                }
            }

        }

    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        String s1 = "acb";
        int n1 = 4;
        String s2 = "ab";
        int n2 = 2;

        Solution solution = new Solution();
        int ret  = solution.getMaxRepetitions(s1, n1, s2, n2);

        // Your Codec object will be instantiated and called as such:
        System.out.printf("ret:%d\n", ret);

        System.out.println();

    }

}
复制代码

 

用了这种Brute Force的方法,居然比我的快。。。。。。

复制代码
public class Solution {
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
        char[] array1 = s1.toCharArray(), array2 = s2.toCharArray();
        int count1 = 0, count2 = 0, i = 0, j = 0;
        
        while (count1 < n1) {
            if (array1[i] == array2[j]) {
                j++;
                if (j == array2.length) {
                    j = 0;
                    count2++;
                }
            }
            i++;
            if (i == array1.length) {
                i = 0;
                count1++;
            }
        }
        
        return count2 / n2;
    }
}
复制代码

 

(完)

 

posted @   blcblc  阅读(593)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示