count-the-repetitions

本文探讨了LeetCode上一道关于字符串重复计数的问题,并提供了几种不同的解决方案。其中包括一种使用额外数组进行优化的方法,以及一个更为简洁的遍历比较方法。尽管进行了优化,但在某些情况下仍会出现超时问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
}

 

(完)

 

<SOCKET-ADDRESS> <SHORT-NAME>SoAddr_ITM_SOC_VLAN_510_UDP</SHORT-NAME> <APPLICATION-ENDPOINT> <SHORT-NAME>AppEndpoint_ITM_SOC_VLAN_510_UDP</SHORT-NAME> <CONSUMED-SERVICE-INSTANCES> <CONSUMED-SERVICE-INSTANCE> <SHORT-NAME>Client_ADC_RX_VehicleCAN_Service_VLAN_510_Instance_1_ITM_SOC_UDP</SHORT-NAME> <CONSUMED-EVENT-GROUPS> <CONSUMED-EVENT-GROUP> <SHORT-NAME>CEG_Event_RawCANDataOfVehicle_Instance_1_ITM_SOC</SHORT-NAME> <APPLICATION-ENDPOINT-REF DEST="APPLICATION-ENDPOINT">/Topology/Clusters/EthernetCluster16/VLAN_510/SoAddr_ITM_SOC_VLAN_510_UDP/AppEndpoint_ITM_SOC_VLAN_510_UDP</APPLICATION-ENDPOINT-REF> <EVENT-GROUP-IDENTIFIER>32769</EVENT-GROUP-IDENTIFIER> <ROUTING-GROUP-REFS> <ROUTING-GROUP-REF DEST="SO-AD-ROUTING-GROUP">/SoAdRoutingGroups/SoAdRG_Event_RawCANDataOfVehicle_ADC_RX_VehicleCAN_Service_Instance_1</ROUTING-GROUP-REF> </ROUTING-GROUP-REFS> <SD-CLIENT-CONFIG> <REQUEST-RESPONSE-DELAY> <MAX-VALUE>0.05</MAX-VALUE> <MIN-VALUE>0.01</MIN-VALUE> </REQUEST-RESPONSE-DELAY> <TTL>3</TTL> </SD-CLIENT-CONFIG> </CONSUMED-EVENT-GROUP> </CONSUMED-EVENT-GROUPS> <PROVIDED-SERVICE-INSTANCE-REF DEST="PROVIDED-SERVICE-INSTANCE">/Topology/Clusters/EthernetCluster16/VLAN_510/SoAddr_ITM_MCU_VLAN_510_UDP/AppEndpoint_ITM_MCU_VLAN_510_UDP/Server_ADC_RX_VehicleCAN_Service_VLAN_510_Instance_1_ITM_MCU_UDP</PROVIDED-SERVICE-INSTANCE-REF> <SD-CLIENT-CONFIG> <CLIENT-SERVICE-MAJOR-VERSION>1</CLIENT-SERVICE-MAJOR-VERSION> <CLIENT-SERVICE-MINOR-VERSION>0</CLIENT-SERVICE-MINOR-VERSION> <INITIAL-FIND-BEHAVIOR> <INITIAL-DELAY-MAX-VALUE>0.05</INITIAL-DELAY-MAX-VALUE> <INITIAL-DELAY-MIN-VALUE>0.01</INITIAL-DELAY-MIN-VALUE> <INITIAL-REPETITIONS-BASE-DELAY>0.05</INITIAL-REPETITIONS-BASE-DELAY> <INITIAL-REPETITIONS-MAX>3</INITIAL-REPETITIONS-MAX> </INITIAL-FIND-BEHAVIOR> <TTL>3</TTL> </SD-CLIENT-CONFIG> </CONSUMED-SERVICE-INSTANCE> </CONSUMED-SERVICE-INSTANCES> <NETWORK-ENDPOINT-REF DEST="NETWORK-ENDPOINT">/Topology/Clusters/EthernetCluster16/VLAN_510/ITM_SOC_VLAN_510</NETWORK-ENDPOINT-REF> <TP-CONFIGURATION> <UDP-TP> <UDP-TP-PORT> <PORT-NUMBER>50101</PORT-NUMBER> </UDP-TP-PORT> </UDP-TP> </TP-CONFIGURATION> </APPLICATION-ENDPOINT> </SOCKET-ADDRESS> 利用python字典数组,编写代码生成上述xml内容
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值