94/104 tests passed. The failure string is too long and being cut off for me to test. Do you see anything wrong with this code


  • 0
    V
    public class Solution {
        public bool RepeatedSubstringPattern(string s) {
            int n = s.Length;
            if(n <= 1) { return false; }
            
            var factors = AllFactors(n);
            foreach(var factor in factors)
            {
                if(IsRepeating(s, factor)) {
                        return true;
                    }
                }
                return false;
        }
        
        public List<int> AllFactors(int num) {
            var list = new List<int>(); 
            if(num == 0) {
                    list.Add(0);
                    return list; 
                
            }
            for(int i = 1; i<= num/2; i++) { 
                if(num % i == 0) {
                        list.Add(i);
                    
                }
            }
            return list;
        }
            
        public  bool IsRepeating(string str, int factor)
        {
            var substr = str.Substring(0, factor);
            var temp = factor;
            do {
                var s = str.Substring(factor,  temp);
                if(s != substr)
                {
                    return false;
                }
    
                factor = 2 * factor;
            } while (factor < str.Length);
    
            return true;
        }
    }
    

  • 0
    V
    Found the issue. In IsRepeating method, it should be factor = factor + temp and not factor = 2 * factor
    public class Solution {
        public bool RepeatedSubstringPattern(string s) {
            int n = s.Length;
    
            if(n <= 1)
            {
                return false;
            }
    
            var factors = AllFactors(n);
            foreach(var factor in factors)
            {
                if(IsRepeating(s, factor))
                {
                    return true;
                }
            }
    
            return false;
        }
        
        public List<int> AllFactors(int num) {
            var list = new List<int>(); 
            for(int i = 1; i <= num/2; i++) { 
                if(num % i == 0) 
                {
                    list.Add(i);
                }
            }
            return list;
        }
            
         public static bool IsRepeating(string str, int factor)
            {
                var substr = str.Substring(0, factor);
    
                var temp = factor;
    
                do
                {
                    var s = str.Substring(factor,  temp);
                    if(s != substr)
                    {
                        return false;
                    }
    
                    factor = ***factor + temp; //2 * factor;***
    
                } while (factor < str.Length);
    
                return true;
            }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.