Split String into Words – Length

The program must accept an integer N, a string S and N integers as the input. The string S contains N words without any space. The N integers represent the length of words in the string S. The program must sort the words in the string S based on their length. If two or more words have the same length, then the program must sort those words in the order of their occurrence. Finally, the program must print the revised string S as the output.

Boundary Condition(s):
2 <= N <= 100
2 <= Length of S <= 10^4
1 <= Each integer value <= 100

Input Format:
The first line contains N.
The second line contains S.
The third line contains N integers separated by a space.

Output Format:
The first line contains the revised string S.

Example Input/Output 1:
Input:
4
lioncattigerrat
4 3 5 3

Output:
catratliontiger

Explanation:
There are 4 words in the given string.
4 -> lion
3 -> cat
5 -> tiger
3 -> rat
After sorting the words based on their length, the string S becomes
catratliontiger

Example Input/Output 2:
Input:
7
applegrapesmangocuckooPineappleNoodlesPizza
5 6 5 6 9 7 5

Output:
applemangoPizzagrapescuckooNoodlesPineapple

n=int(input());a=input().strip()
l=list(map(int,input().split()))
ans=[]
for i in l:
    ans.append(a[:i])
    a=a[i:]
ans.sort(key=len)
print(*ans,sep="")
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,i,j,k,a[101],p=0,t;
    char s[10001],d[1000][1000];
    scanf("%d",&n);
    scanf("%s",s);
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
        for(j=0;j<a[i];j++){
            d[i][j]=s[p++];
        }
        //p=a[i];
    }
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            if(a[i]>a[j]){
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(a[i]==strlen(d[j])){
                printf("%s",d[j]);
                d[j][0]='';
                break;
            }
        }
    }
}
#include <bits/stdc++.h>

using namespace std;
vector<string> z;
vector<int> d;
bool len(int x,int y){
    if(z[x].size()!=z[y].size()){
        return z[x].size()<z[y].size();
    }
    return x<y;
}
int main(int argc, char** argv)
{
    int n;
    string s;
    cin>>n>>s;
    int a[n];
    string t=s;
    for(int i=0;i<n;i++){
        cin>>a[i];
        z.push_back(t.substr(0,a[i]));
        d.push_back(i);
        t=t.substr(a[i]);
    }
    sort(d.begin(),d.end(),len);
    for(int i:d)
    cout<<z[i];
}
import java.util.*;
public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String str = sc.next();
        TreeMap < Integer, ArrayList < String >> map1 = new TreeMap < > ();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            String a = str.substring(0, x);
            str = str.substring(x);
            if (map1.containsKey(x)) {
                ArrayList < String > cur = map1.get(x);
                cur.add(a);
                map1.put(x, cur);

            } else {
                ArrayList < String > cur = new ArrayList < > ();
                cur.add(a);
                map1.put(x, cur);
            }
        }
        for (int i: map1.keySet()) {
            ArrayList < String > cur = map1.get(i);
            for (String j: cur) {
                System.out.print(j);
            }
        }
    }
}
Previous Article

function printTimeInSeconds with Varargs

Next Article

Non-Isomorphic Numbers

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *