Two Characters Hackerrank Problem’s Solution

Two Characters Hackerrank

There are two characters, take it as x & y.We have different strings which can be made through these characters in an alternate way like xyxyxy or yxyxyx but not xxyy or xyyx or so on

 

Now, there is a twist that you can convert some string ‘s’ to string ‘t’ by deleting characters from ‘s’. When you delete a character from s, you must delete all occurrences of it in s. For example, if s = abaacdabd and you delete the character a, then the string becomes bcdbd.

Suppose you have provided a string S, convert it to the possible longest String T using the same alphabet then print the length of the newly string T in a new Line otherwise if the string T is not possible to form then print its length to 0.

Problem: https://coderinme.com/hackerrank-two-characters/

The problem is of hackerrank to go to that use the below link:

https://www.hackerrank.com/challenges/two-characters

  • Input Format

The first line contains a single integer denoting the length of s.

The second line contains string s.

 

  • Constraints
    1<=|s|<=1000
    Here, an only contains lowercase English alphabetic letters (i.e., a to z).

 

  • Output text

Print a single integer denoting the maximum length of t for the given s; if it is not possible to form string t, print 0 instead.

 

Sample Input

10
beabeefeab
Sample Output

5

 

Explanation

  • The characters present in s are a, b, e, and f. This means that must consist of two of those characters.
  • If we delete e and f, the resulting string is babab. This is a valid t as there are only two distinct characters (a and b), and they are alternating within the string.
  • If we delete a and f, the resulting string is bebeeeb. This is not a valid string t because there is three consecutive e’s present.
  • If we delete the only e, the resulting string is babfab. This is not a valid string t because it contains three distinct characters.

Thus, we print the length of babab, which is 5, as our answer.

Solution

Case: s only consists of 1 distinct character.Because string t must contain exactly two distinct characters that alternate throughout the string, we cannot form t in this case so the answer will be 0.
Case: s consists of two or more distinct characters.
Choose two characters to be present in string t and delete all other characters from s.
Check the resulting string to see if it’s a valid t. In other words, determine if the two characters alternate or not. If yes, then the string is valid and you simply need to simply save its length; otherwise, it’s invalid and we consider its length to be 0.
Check all possible pairs of characters and then print the maximum of these lengths.

 

#!/bin/python3

from itertools import combinations
#include <bits/stdc++.h>
#include<assert.h>
using namespace std;

int make_string(string str, char a, char b)
{
    int len = str.size();
    string temp = "";
    char last;

    for(int i=0; i<len; i++)
    {
        if(str[i]== a || str[i] == b)
            temp+=str[i];
    }

    len = temp.size();
    if(len <= 1)
        return 0;

    last = temp[0];
    for(int i=1; i<len; i++)
    {
        if(temp[i] == last)
            return 0;
        last = temp[i];
    }
    return len;
}

int main(){
string s; char a, b;
cin>>s;
cin>>a>>b;

cout<<make_string(s, a, b);
return 0;
}

 

def meet_pattern(s):
return all(s[i-1] != s[i] for i in range(1,len(s)))

s_len = int(input().strip())
s = input().strip()
letters = set(s)
max_len = 0
for pair in combinations(letters,2):
substr = "".join(i for i in s if i in pair)
if meet_pattern(substr):
max_len = max(max_len, len(substr))
print(max_len)

 

 

import sys
#sys.stdin=open("in","r")
n=int(raw_input())
s=raw_input()
assert s.isalpha()
ans=0
for i in range(0,26):
    for j in range(0,26):
        if i==j:
            continue
        p1 = i
        p2 = j
        flag = 1
        l = 0
        for c in s:
            if ord(c)-ord('a')!=p1 and ord(c)-ord('a')!=p2:
                continue
            if ord(c)-ord('a') == p1:
                l = l + 1
                p1,p2 = p2,p1
            else:
                flag = 0
        if flag == 1 and l>1:
            ans=max(ans,l)

print ans

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi