WAP to check if one string is permutation of other !!


  • 0
    J

    Q:

    WAP to check if one string is permutation of other.

    Example:

    {'d','e','b','u','g'} is permutation of {'b','u','g','d','e'}

    {'d','e','b','u','g'} is not permutation of {'e','r','r','d','e'}

    Solution:
    1st- True, if difference of both array ASCII value is ZERO.

    2nd- Sort both the array and compare sorted version of array elements.

    Time Complexity Big O:
    O(n)

    Space Complexity Big O:
    O(1)

    Code:

    bool isPermutation(char *str1, int len1, char *str2, int len2)
    {
        if(len1!=len2 || str1==NULL || str2 == NULL) return false;
        
        int i=0, diff=0; //Assumption , diff of two array element may cross int MAX value. 
        
        do{
            diff += (str1[i] - str2[i]);
        }while(++i<len1);
        
        if(diff)
            return false;
        else
            return true;
    }

  • 0
    I

    What does WAP mean?


  • 0

    @IWantToPass perhaps Whether a permutation :-)


  • 0

    @IWantToPass @elmirap Haha. It means Write a Program. A lot of people use it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.