Given two strings check whether first string could be converted to the second string but the conditions are:
1.If the character is at odd place you can swap this character with the characters only at the odd places in    the given first string.
2.If the character is at even place you can swap this character with the characters only at the even places      in the given first string.
3.You cannot insert or delete any character on your own.
 
Example 1:
Input: geeks for
Output: 0
Explanation: Two strings are of
different length. They can't be equal.
Example 2:
Input: skeeg geeks
Output: 1
Explanation:
In first string characters at odd indices are 
's', 'e', 'g'
In second string characters at odd indices are 
'g', 'e', 's'
After swaping index 1st and 3rd of first
string all the characters at odd positions 
become equal.
Same for even indices.
 
Your Task:
You don't need to read or print anything. Your task is to complete the function EqualString() which takes first string as first parameter and second string as second parameter and returns 1 if first string can be made equal to the second string otherwise returns 0.
 
Expected Time Complexity: O(n)
Expected Space Complexity: O(K) where K is constant.
 
Constraints:
1 <= length of both strings <= 1000

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3





class Solution:

    def EqualString(self, s, t):

        # Code here

Solutions:
class Solution:

	def EqualString(self, s, t):
		if len(s) != len(t):
			return 0
		else:
			(a, b, c, d) = ([], [], [], [])
			n = len(s)
			for i in range(0, n, 2):
				a.append(s[i])
				b.append(t[i])
			for i in range(1, n, 2):
				c.append(s[i])
				d.append(t[i])
			a.sort()
			b.sort()
			c.sort()
			d.sort()
			if a == b and c == d:
				return 1
			else:
				return 0


from collections import defaultdict as dd

class Solution:

	def EqualString(self, s, t):
		n = len(s)
		if len(t) != n:
			return 0
		store = dd(lambda : 0)
		for i in range(0, n, 2):
			store[s[i]] += 1
			store[t[i]] -= 1
		if set(store.values()) != set([0]):
			return 0
		for i in range(1, n, 2):
			store[s[i]] += 1
			store[t[i]] -= 1
		return int(set(store.values()) == set([0]))


class Solution:

	def EqualString(self, s, t):
		if sorted(s[0::2]) == sorted(t[0::2]) and sorted(s[1::2]) == sorted(t[1::2]):
			return 1
		return 0