Breaking

Tuesday 26 September 2017

String - Repeating Alphabets

                    String - Repeating Alphabets



Given a string S as the input, print the distinct alphabets in S that occur more than once. The alphabets must be printed based on the order of their occurrence in S.
Input Format:
The first line contains S.
Output Format:
The first line contains the distinct alphabets in S that occur more than once.
Boundary Conditions:
2 <= LENGTH of S <= 200
Example Input/Output 1:
Input:
Apple
Output:
p
Example Input/Output 2:
Input:
environment
Output:
en
Code:

Java:
import java.util.*;
public class Hello {
    public static void main(String[] args) {
                   Scanner sc=new Scanner(System.in);
                   String str=sc.next();
                   int i;
                   List<Character> li=new LinkedList<Character>();
                   Set<Character> st=new LinkedHashSet<Character>();
                   for(i=0;i<str.length();i++)
                   {
                       li.add(str.charAt(i));
                       st.add(str.charAt(i));
                   }
                   for(Character c: st)
                   {
                       if(Collections.frequency(li,c)>1)
                           System.out.print(c);
                   }
          }
}



C++:

#include<iostream>
#include<string.h>
using namespace std;

int main(int argc, char** argv)
{
    char s[200];
    cin>>s;
    int n=strlen(s);
    int i,j,f=0;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n&&s[i]!='*';j++)
        {
            if(s[i]==s[j])
            {
                s[j]='*';
                f=1;
            }
        }
        if(f==1)
        {
            cout<<s[i];
            f=0;
        }
    }
}


1 comment:

  1. PYTHON 3
    s=input()
    op=''
    for i in s:
    if(s.count(i)>1):
    if(i not in op):
    op+=i
    print(op)

    ReplyDelete

Like