Sort by

recency

|

18 Discussions

|

  • + 0 comments

    An informal proof of some sort:

    When removing at only 1 position, which positions when removed will not be distinct? Take any 2 positions , when would they be equal? We get the following equalities:

    for all

    for all ()

    for all

    This shows that 2 positions are only equal if they are on the same equal group. So now we try to count by going over groups instead.

    What about removing at only 2 positions? Again, take any 2 positions in different groups . Without loss of generality: and . When , and would have to be in the same group so we ignore this case. When things get a little interesting (note that case is the same as the only removing 1 character case):

    for all

    for all ()

    for all ( and )

    for all ()

    for all

    Unioning these equalities, if represents any character and and represent any 2 distinct characters, the string has the form: or . Ignoring equal groups, this means removing at adjacent positions on the same alternating character group will result in the same string.

    My solution first counts the number of ways to choose 2 groups plus choosing 2 characters in the same group, then subtracts the length of each alternating character group minus 1 so that adjacent positions are only counted once per alternating character group to account for the overcounting.

  • + 0 comments

    C++ Solution :

    long beautifulStrings(string S) {
         unordered_set<string> beautifulStrings;
    
        for (int i = 0; i < S.length(); i++) {
            for (int j = i + 1; j < S.length(); j++) {
                string candidate = S.substr(0, i) + S.substr(i + 1, j - i - 1) + S.substr(j + 1);
                beautifulStrings.insert(candidate);
            }
        }
    
        return beautifulStrings.size();
    
    }
    

    Note : Only 3 test case passed.

  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Beautiful Strings Problem Solution

  • + 0 comments

    Here is Beautiful Strings problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-beautiful-strings-problem-solution.html

  • + 0 comments
    // this solution without any type of container
    #include<iostream>
    
    typedef int usg_t;
    
    int main()
    {
    	char x, y, z;
    	usg_t dup{ 0 }, dupc{ 0 }, tria{ 0 }, dis{ 0 };
    	// dup count for X...XX // tria count for XOX // dis count for XO .
    	x = getchar(); y = getchar(); z = getchar();
    	if ((x < 'a' || x > 'z') || (y < 'a' || y > 'z'))
    		std::cout << 0, exit(0);
    	else if (z < 'a' || z > 'z')
    			std::cout << 1, exit(0);
    	if (x == y)
    	{
    		if (x == z)
    			dis ++, dup++, ++dupc;
    		else dup++, dis += 2;
    	}
    	else {
    		if (x == z)
    			dis += 3, tria++;
    		else if (y == z)
    			dis += 2, dup++, dupc++;
    		else
    			dis += 3;
    	}
    	while (true)
    	{
    		x = y, y = z;
    		z = getchar();
    		if (z < 'a' || z > 'z')
    			break;
    		if (x == y) 
    		{
    			if (x != z)
    				++dis;
    		}
    		else 
    		{
    			if  (x != z) 
    			{
    				if (y == z)
    					++dup;
    				else
    					++dis;
    			}
    			else
    				dis++, tria++;
    		}
    	}
    	std::cout << ((dis * (dis - 1) / 2) + dup - tria) << std::endl;
    	system("pause");
    }