You are viewing a single comment's thread. Return to all comments →
C# code
HashSet<long> arePerfectNums = new HashSet<long>(); for(string newS = s.Substring(0,1), tempS = s.Substring(1); tempS.Length > 0 && newS.Length <= tempS.Length; newS += tempS.Substring(0,1), tempS = tempS.Substring(1)) { if (long.TryParse(newS, out long testNum) && $"{testNum + 1}".Equals(tempS.Substring(0, $"{testNum + 1}".Length))) { bool isPerfectNum = true; string remain = tempS; for (string InewS = newS; remain.Length > 0 && InewS.Length <= remain.Length && isPerfectNum;) { if (long.TryParse(InewS, out long testNum2) && $"{testNum2 + 1}".Equals(remain.Substring(0, $"{testNum2 + 1}".Length))) { InewS = remain.Substring(0, $"{testNum2 + 1}".Length); remain = remain.Substring($"{testNum2 + 1}".Length); } else isPerfectNum = false; } if (isPerfectNum && remain.Length == 0) arePerfectNums.Add(testNum); } } Console.WriteLine(arePerfectNums.Count > 0 ? $"YES {arePerfectNums.First()}" : "NO");
Seems like cookies are disabled on this browser, please enable them to open this website
Separate the Numbers
You are viewing a single comment's thread. Return to all comments →
C# code