- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check whether the given strings are isomorphic using C#?
Two strings, X and Y, are called isomorphic if all occurrences of each character in X can be replaced with another character to get Y and vice-versa. For example, consider strings ACAB and XCXY. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example 1
Input − s = "egg", t = "add"
Output − true
Example 2
Input − s = "foo", t = "bar"
Output − false
Time complexity − O(N)
Space complexity − O(N)
code
public class Arrays{ public bool IsStringIsomorphic(string s, string t){ if (s == null || t == null){ return false; } int[] chars1 = new int[128]; int[] chars2 = new int[128]; for (int i = 0; i < s.Length; i++){ if (chars1[s[i]] != chars2[t[i]]){ return false; } else{ chars1[s[i]] = i + 1; chars2[t[i]] = i + 1; } } return true; } } static void Main(string[] args){ Console.WriteLine(s.IsStringIsomorphic("add", "egg")); }
Output
True
Advertisements