Amazon
  • Introduction
  • Phone Interview I
    • 53.Reverse Words in a String
    • 31.Partition Array
  • Phone Interview II
    • 167.Add Two Numbers
    • 88.Lowest Common Ancestor
  • Onsite I
    • 655.Big Integer Addition
    • 221.Add Two Numbers II
  • Onsite II
    • 158.Two Strings Are Anagrams
    • 171.Anagrams
    • 386.Longest Substring with At Most K Distinct Characters
  • Onsite III
    • 479.Second Max of Array
    • 589.Connecting Graph
  • Onsite IV
    • 532.Reverse Pairs
  • 2022
    • OA
      • work simulation
      • Greyness
      • NearestRetailer
      • Sum of Scores of Subarray
      • StrengthOfPassword
      • ProductOf1
      • Move 0/1 InArray
      • Max deviation among all substrings
      • AWS power consumption
      • searchWordResultWord
      • maxOperationOfString
      • MinHealthGame
      • EarliestMonth
      • Package ship
      • RatingConsectiveDecresing
      • LinkedListSum
      • MovingBoxes
      • ValidString
      • MaxValueAfterRemovingFromString
      • Subtree with Maximum Average
    • VO
      • 2022.3
    • BQ
      • doc
      • 2022.4
      • Freq Question
      • 11大类BQ和Follow-ups
      • Page 1
      • BQ 100
      • 35 behavioral questions asked in 95% of Amazon interviews with examples
      • Page 2
      • 反向BQ
    • LP
      • LP-1
      • LP-2
    • SD
      • Design Amazon Prime Video Recommendation System
      • Amazon Order system
    • OOD
      • Linux Find Command
      • Amazon Locker
    • AWS Identity call
    • Interviews
Powered by GitBook
On this page

Was this helpful?

  1. 2022
  2. OA

StrengthOfPassword

PreviousSum of Scores of SubarrayNextProductOf1

Last updated 3 years ago

Was this helpful?

这道题 但是细节稍有不同 这题用nested for loop就可以解但会有test cases超时 需要用DP比较快就全能过了.

*

Approach: Take all possible sub-strings of the given string and use a to check whether the current sub-string has been processed before. Now, for every distinct sub-string, count the distinct characters in it (again set can be used to do so). The sum of this count for all the distinct sub-strings is the final answer. Below is the implementation of the above approach:

class geeks
{
 
    // Function to return the count of distinct
    // characters in all the distinct
    // sub-strings of the given string
    public static int countTotalDistinct(String str)
    {
        int cnt = 0;
 
        // To store all the sub-strings
        HashSet<String> items = new HashSet<>();
 
        for (int i = 0; i < str.length(); ++i)
        {
 
            // To store the current sub-string
            String temp = "";
 
            // To store the characters of the
            // current sub-string
            HashSet<Character> ans = new HashSet<>();
            for (int j = i; j < str.length(); ++j)
            {
                temp = temp + str.charAt(j);
                ans.add(str.charAt(j));
 
                // If current sub-string hasn't
                // been stored before
                if (!items.contains(temp))
                {
 
                    // Insert it into the set
                    items.add(temp);
 
                    // Update the count of
                    // distinct characters
                    cnt += ans.size();
                }
            }
        }
 
        return cnt;
    }

Idea : counting the contribution by each alphabet to the answer i.e, If a substring has multiple a's, only '1' is contributed by 'a' to the final count per each substring.

    int solve(char c, string s){ // Counts no.of substrings that contain atleast one of the characters as c.
        int cnt=0,n=s.length(),res=0;
        for(int i=0;i<n;i++){
            if(s[i]==c){
                res+=(cnt)*(cnt+1)/2;
                cnt=0;
            }
            else
                cnt++;
        }
        res+=(cnt)*(cnt+1)/2;
        return n*(n+1)/2 - res;
    }
    int countdistinct(string s) {
        int ans=0;
        for(char c='A';c<='Z';c++)
            ans+=solve(c,s);
        return ans;
    }

Version 2:

https://leetcode.com/discuss/interview-question/1481915/Amazon-or-OA-or-Count-distinct-characters-in-all-substrings
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/
https://leetcode.com/discuss/interview-question/1481915/Amazon-or-OA-or-Count-distinct-characters-in-all-substrings
https://www.tutorialspoint.com/count-unique-characters-of-all-substrings-of-a-given-string-in-cplusplus
https://leetcode.com/discuss/interview-question/1564287/Amazon-or-OA-or-Strength-of-password/1143344
https://www.geeksforgeeks.org/find-distinct-characters-in-distinct-substrings-of-a-string/
set