|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "Check Permutation\n", |
| 10 | + "--------------------\n", |
| 11 | + "Given two strings, S and T, check if they are permutations of each other. Return true or false.\n", |
| 12 | + "Permutation means - length of both the strings should same and should contain same set of characters. Order of characters doesn't matter.\n", |
| 13 | + "Note : Input strings contain only lowercase english alphabets.\n", |
| 14 | + "\n", |
| 15 | + "\n", |
| 16 | + "Input format :\n", |
| 17 | + "Line 1 : String 1\n", |
| 18 | + "Line 2 : String 2\n", |
| 19 | + "Output format :\n", |
| 20 | + "'true' or 'false'\n", |
| 21 | + "Constraints :\n", |
| 22 | + "0 <= |S| <= 10^7\n", |
| 23 | + "0 <= |T| <= 10^7\n", |
| 24 | + "where |S| represents the length of string, S.\n", |
| 25 | + "Sample Input 1 :\n", |
| 26 | + "abcde\n", |
| 27 | + "baedc\n", |
| 28 | + "Sample Output 1 :\n", |
| 29 | + "true" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": 1, |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [ |
| 37 | + { |
| 38 | + "name": "stdout", |
| 39 | + "output_type": "stream", |
| 40 | + "text": [ |
| 41 | + "abcde\n", |
| 42 | + "baedc\n", |
| 43 | + "true\n" |
| 44 | + ] |
| 45 | + } |
| 46 | + ], |
| 47 | + "source": [ |
| 48 | + "def arePermutation(str1, str2):\n", |
| 49 | + " \n", |
| 50 | + " n1 = len(str1) \n", |
| 51 | + " n2 = len(str2)\n", |
| 52 | + " if (n1 != n2):\n", |
| 53 | + " return False\n", |
| 54 | + " \n", |
| 55 | + " # Sort both strings \n", |
| 56 | + " a = sorted(str1) \n", |
| 57 | + " str1 = \"\".join(a) \n", |
| 58 | + " b = sorted(str2) \n", |
| 59 | + " str2 = \"\".join(b) \n", |
| 60 | + " \n", |
| 61 | + " # Compare sorted strings \n", |
| 62 | + " for i in range(0, n1, 1): \n", |
| 63 | + " if (str1[i] != str2[i]): \n", |
| 64 | + " return False\n", |
| 65 | + " \n", |
| 66 | + " return True\n", |
| 67 | + " \n", |
| 68 | + "str1 = input()\n", |
| 69 | + "str2 = input()\n", |
| 70 | + "if (arePermutation(str1, str2)):\n", |
| 71 | + " print(\"true\") \n", |
| 72 | + "else: \n", |
| 73 | + " print(\"false\") " |
| 74 | + ] |
| 75 | + } |
| 76 | + ], |
| 77 | + "metadata": { |
| 78 | + "kernelspec": { |
| 79 | + "display_name": "Python 3", |
| 80 | + "language": "python", |
| 81 | + "name": "python3" |
| 82 | + }, |
| 83 | + "language_info": { |
| 84 | + "codemirror_mode": { |
| 85 | + "name": "ipython", |
| 86 | + "version": 3 |
| 87 | + }, |
| 88 | + "file_extension": ".py", |
| 89 | + "mimetype": "text/x-python", |
| 90 | + "name": "python", |
| 91 | + "nbconvert_exporter": "python", |
| 92 | + "pygments_lexer": "ipython3", |
| 93 | + "version": "3.7.6" |
| 94 | + } |
| 95 | + }, |
| 96 | + "nbformat": 4, |
| 97 | + "nbformat_minor": 4 |
| 98 | +} |
0 commit comments