We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Beautiful Strings
  5. Discussions

Beautiful Strings

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 15 Discussions, By:

recency

Please Login in order to post a comment

  • thecodingsoluti2
    3 months ago+ 0 comments

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

    0|
    Permalink
  • Nguyen_Thanh_Duy
    2 years ago+ 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");
    }
    
    0|
    Permalink
  • maheshrachha1225
    3 years ago+ 0 comments

    def beautifulStrings(s): perms=permutations(s,2) return len(set(perms))

    How Can I pass all the cases ? its just passing single test case... can Anyone give the idea or hints please??

    0|
    Permalink
  • jEeTu_pYthOn
    4 years ago+ 3 comments

    my python 3 approach some test cases are passed but some are terminated due to timeout error can anyone help:

    from itertools import combinations
    a=input()
    p=combinations(a,len(a)-2)
    p=set(p)
    print(len(p))
    
    0|
    Permalink
  • mato_kolesar23
    5 years ago+ 1 comment
    from itertools import permutations
    
    string=str(input())
    perms = [''.join(p) for p in permutations(string)]
    
    
    for i in range(len(perms)):
        perms[i]=perms[i][:2]
    perms= list(set(perms))
    print(len(perms))
    

    Anyone knows why its giving me runtime errors ? I tried it on my own pc and it was working just fine!

    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy