Compare two alphanumeric string in python
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article Given two strings that can contain lower and uppercase alphabets, numbers and special characters like dots, blank spaces, commas, etc. Compare both strings considering only alphanumeric characters([a-b], [A-B] and [0-9]) if they are equal or not. For example, strings “Ram, Shyam” and “Ram-Shyam” both are the same and also “/.’;[]” and “@# >” are same. Examples: Input: str1 = "Ram, Shyam", str2 = " Ram - Shyam." Output: Equal Explanation: if we ignore all characters except alphanumeric characters then strings will be, str1 = "RamShyam" and str2 = "RamShyam". Therefore both strings are equal. Input : str1 = "aaa123", str2 = "@aaa-12-3" Output : Equal Input : str1 = "abc123", str2 = "123abc" Output : Unequal Explanation: In this, str1 = "abc123" and str2 = "123abc". Therefore both strings are not equal. Since we have to compare only alphanumeric characters therefore whenever any other character is found simply ignore it by increasing iterator pointer. For doing it simply take two integer variables i and j and initialize them by 0. Now run a loop to compare each and every character of both strings. Compare one by one if the character is alphanumeric otherwise increase the value of i or j by one. Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Time Complexity: O(n*n) How do you compare two alphanumeric strings in Python?Input : str1 = "aaa123", str2 = "@aaa-12-3" Output : Equal Input : str1 = "abc123", str2 = "123abc" Output : Unequal Explanation: In this, str1 = "abc123" and str2 = "123abc". Therefore both strings are not equal. Recommended: Please try your approach on {IDE} first, before moving on to the solution.
How do you compare alphanumeric?An alphanumeric comparison is a comparison of the single-byte character values of two operands.. For the EBCDIC character set, the EBCDIC collating sequence is used.. For the ASCII character set, the ASCII collating sequence is used.. How do you compare two letters in a string?strcmp is used to compare two different C strings. When the strings passed to strcmp contains exactly same characters in every index and have exactly same length, it returns 0. For example, i will be 0 in the following code: char str1[] = "Look Here"; char str2[] = "Look Here"; int i = strcmp(str1, str2);
How do you check if two strings are alphabetical order in Python?Comparing Strings with <, >, <=, and >=
To compare strings alphabetically, you can use the operators <, >, <=, >=.
|