Techno Blender
Digitally Yours.

Number of pairs of String whose concatenation leads to a Sorted string

0 32


#include <bits/stdc++.h>

using namespace std;

  

bool sorted(string s)

{

  

    for (int i = 0; i < s.size() - 1; i++) {

        if (s[i] > s[i + 1])

            return false;

    }

    return 1;

}

  

int solve(string S[], int N)

{

  

    

    

    

    bool mark[N + 1] = { 0 };

  

    for (int i = 0; i < N; i++) {

        if (sorted(S[i])) {

            mark[i] = 1;

        }

    }

    

    

    

    int nums[26] = { 0 };

  

    for (int i = 0; i < N; i++) {

  

        if (mark[i] == 1) {

            int p = S[i][0] - 'a';

            nums[p] += 1;

        }

    }

  

    

    

    int ans = 0;

    for (int i = 0; i < N; i++) {

        if (mark[i] == 1) {

            int len = S[i].size();

            int last_char = S[i][len - 1] - 'a';

  

            for (int j = last_char; j < 26; j++) {

                ans += nums[j];

            }

        }

    }

  

    

    return ans;

}

  

int main()

{

  

    

    string S[] = { "ac", "df", "pzz" };

    int N = sizeof(S) / sizeof(S[0]);

  

    

    cout << solve(S, N) << endl;

  

    

    string S2[] = { "pqrs", "amq", "bcd" };

    N = sizeof(S2) / sizeof(S2[0]);

  

    

    cout << solve(S2, N) << endl;

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

  

bool sorted(string s)

{

  

    for (int i = 0; i < s.size() - 1; i++) {

        if (s[i] > s[i + 1])

            return false;

    }

    return 1;

}

  

int solve(string S[], int N)

{

  

    

    

    

    bool mark[N + 1] = { 0 };

  

    for (int i = 0; i < N; i++) {

        if (sorted(S[i])) {

            mark[i] = 1;

        }

    }

    

    

    

    int nums[26] = { 0 };

  

    for (int i = 0; i < N; i++) {

  

        if (mark[i] == 1) {

            int p = S[i][0] - 'a';

            nums[p] += 1;

        }

    }

  

    

    

    int ans = 0;

    for (int i = 0; i < N; i++) {

        if (mark[i] == 1) {

            int len = S[i].size();

            int last_char = S[i][len - 1] - 'a';

  

            for (int j = last_char; j < 26; j++) {

                ans += nums[j];

            }

        }

    }

  

    

    return ans;

}

  

int main()

{

  

    

    string S[] = { "ac", "df", "pzz" };

    int N = sizeof(S) / sizeof(S[0]);

  

    

    cout << solve(S, N) << endl;

  

    

    string S2[] = { "pqrs", "amq", "bcd" };

    N = sizeof(S2) / sizeof(S2[0]);

  

    

    cout << solve(S2, N) << endl;

    return 0;

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment