Breaking

Sunday 10 September 2017

String Letters Frequency


String Letters Frequency

A string value S containing N unique letters is passed as the input. The program must print the letters in the string based on the count of their occurrence. The letters of higher frequency of occurrence must appear first. If two letters have same frequency of occurrence then they are arranged as per alphabetical order.
Input Format:
The first line contains S.
Output Format:
N lines containing letters based on their frequency of occurrence.
Boundary Conditions:
2 <= LENGTH(S) <= 10000
Example Input/Output 1:
Input:
MANAGEMENT
Output:
A2
E2
M2
N2
G1
T1
Example Input/Output 2:
Input:
ArrangemENt
Output:
r2
A1
E1
N1
a1
e1
g1
m1
n1
t1
Code:

C++:

#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char is[10000];
int i,m,c[256]={0};
cin>>is;
for(i=0;is[i]!='\0';i++)
{
    c[is[i]]++;
}
m=50;
while(m!=0)
{
for(i=0;i<256;i++)
{
    if(c[i]!=0 && c[i]==m)
    {
        cout<<(char)i<<c[i]<<"\n";
        c[i]=0;
    }
}
m=m-1;
}
return 0;
}





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;
    Set<Character> st=new LinkedHashSet<Character>();
    List<Character> li=new ArrayList<Character>();
    for(i=0;i<str.length();i++)
    {
        st.add(str.charAt(i));
        li.add(str.charAt(i));
       
    }
    //String res[]=new String[st.size()];
    int freq[]=new int[st.size()];
    char ch[]=new char[st.size()];
    i=0;
    for(Character c:st)
    {
     //   res[i]=Character.toString(c)+Integer.toString(Collections.frequency(li,c));
        freq[i]=Collections.frequency(li,c);
        ch[i]=c;
        i++;
    }
   
    int j,temp;
    char cc;
             //sorting based on character and frequency using bubble sort

     for(i=0;i<st.size()-1;i++)  
   {
        for(j=0;j<st.size()-i-1;j++)
        {
            if(freq[j]==freq[j+1])
            {
            if(ch[j]>ch[j+1])
            {
            temp=freq[j];
            freq[j]=freq[j+1];
            freq[j+1]=temp;
            cc=ch[j];
            ch[j]=ch[j+1];
            ch[j+1]=cc;
            }
            }
       else if(freq[j]<freq[j+1])
            {
                        temp=freq[j];
                        freq[j]=freq[j+1];
                        freq[j+1]=temp;
                        cc=ch[j];
                        ch[j]=ch[j+1];
                        ch[j+1]=cc;
            }
        }
    }
    for(i=0;i<st.size();i++)
        {
            System.out.println(ch[i]+""+freq[i]);
        }
}
}


Please do comment If u have any Queries!

4 comments:

  1. import java.util.*;
    public class Hello {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    char[] str=sc.next().toCharArray();
    int n=str.length;
    int max=0,i=0;
    Arrays.sort(str);
    Set st=new LinkedHashSet();
    List li=new ArrayList();
    for(i=0;i0;i--){
    for(int j=0;j<st.size();j++){
    if(freq[j]==i)
    System.out.println(ch[j]+""+freq[j]);
    }
    }
    }
    }

    ReplyDelete
    Replies
    1. import java.util.*;
      public class Hello {
      public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      char[] str=sc.next().toCharArray();
      int n=str.length;
      int max=0,i=0;
      Arrays.sort(str);
      Set st=new LinkedHashSet();
      List li=new ArrayList();
      for(i=0;i0;i--){
      for(int j=0;j<st.size();j++){
      if(freq[j]==i)
      System.out.println(ch[j]+""+freq[j]);
      }
      }
      }
      }

      //this is full program

      Delete
    2. only half is being pasted.. sorry

      Delete
  2. #include
    #include

    int main()
    {
    char s[10000];
    scanf("%s",s);
    int l=strlen(s);
    int freq[255]={0};
    for(int i=0;i=1;j--)
    {
    for(int i=0;i<255;i++)
    {
    if(freq[i]==j)
    printf("%c%d\n",i,freq[i]);
    }
    }

    }

    ReplyDelete

Like