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.
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to remove all non-alphanumeric characters from a string in Java
- Generate a string from an array of alphanumeric strings based on given conditions
- Compare two strings lexicographically in Java
- C++ program to compare two Strings using Operator Overloading
- C program to Compare Two Strings without using strcmp() function
- Maximize a number considering permutations with values smaller than limit
- Alphanumeric Abbreviations of a String
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
- How to check string is alphanumeric or not using Regular Expression
- Count alphanumeric palindromes of length N
- Compare two Version numbers
- C++ Program to compare two string using pointers
- Strings formed from given characters without any consecutive repeating characters
- Minimum swaps to make two strings equal by swapping only with third string
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Interleaving of two given strings with no common characters
- Find uncommon characters of the two strings
- Least number of manipulations needed to ensure two strings have identical characters
- Concatenated string with uncommon characters of two strings
- Count common characters in two strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.