Extraordinary Substrings
Updated
Problem statement
Each lowercase English letter has a value:
abmap to1.cdemap to2.fghmap to3.ijkmap to4.lmnmap to5.opqmap to6.rstmap to7.uvwmap to8.xyzmap to9.
A nonempty contiguous substring is extraordinary when the sum of its letter values is divisible by its length.
Return the number of extraordinary substrings of input_str. Count occurrences at different positions separately, even when their text is equal.
Function
countSubstrings(input_str: String) → intExamples
Example 1
input_str = "bdh"return = 4Each single letter is extraordinary. The full string has values 1, 2 and 3, with sum 6 divisible by length 3. The pairs have sums 3 and 5, so neither qualifies. There are 4 occurrences.
Example 2
input_str = "asdf"return = 6The four single letters qualify. The pair as has sum 8 and length 2; sdf has sum 12 and length 3. The other pairs, asd and the full string fail divisibility. There are 6 occurrences.
Example 3
input_str = "abc"return = 4The three single letters qualify. The pair ab has sum 2 and length 2. The pair bc has sum 3 and the full string has sum 4, so neither qualifies. There are 4 occurrences.
Constraints
1 <= input_str.length <= 2000.input_strcontains only lowercase English lettersathroughz.
More Palantir Online Assessment questions
- Basic Item DiscountsOA · Seen Nov 2026
- Optimize Discount Selection Under a Point BudgetOA · Seen Nov 2026
- Stacked Item and Category DiscountsOA · Seen Nov 2026
- Minimize Path ValueOA · Seen Sep 2024
- No Pairs AllowedOA · Seen Sep 2024
- REST API: Country CodesOA · Seen Feb 2024
- Swap ParityOA · Seen Feb 2024
- Freelance Candidate ReviewOA · Seen Nov 2023
- Benchmarking Tool ReportOA · Seen Sep 2023
- Count Fit Records Across PagesOA · Seen Sep 2023
Similar questions at other companies
- Shortest Digit Prefix for Target MultisetsVisa · OAVisa interview questions
- Longest Balanced Substring After One SwapTekion · OATekion interview questions
- Count Special StringsAmazon · OA
- Longest Substring with Even OccurrencesDeloitte · OADeloitte interview questions
- Number of Divisible Substrings (For MLE)Wayfair · OAWayfair interview questions
- Longest Equal-Frequency SubstringDRW · PHONE SCREENDRW interview questions
- Weighted Next-Word Prediction From Training SequencesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
- Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions