In a native language, the increasing order of priority of characters is a, b, c, d, e, f, g, h, i, j, k, l, m, n, ng, o, p, q, r, s, t, u, v, w, x, y, z. You are given two strings s1 and s2 and your task is to compare them on the basis of the given priority order.
Note: Function must return 0 if both the strings are equal, 1 if s1 is greater than s2, and -1 if s1 is lesser than s2.
Example 1:
Input: s1 = "adding", s2 = "addio"
Output: -1
Explanation: 'o' has greater priority 
than 'ng'
Example 2:
Input: s1 = "abcng", s2 = "abcno"
Output: 1
Explanation: 'ng' has greater priority 
than 'n'
Your Task:  
You don't need to read input or print anything. Your task is to complete the function stringComparsion() which takes the two strings as input and returns the integer value.
Expected Time Complexity: O(|s1 + s2|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |s1|, |s2| ≤ 105
The string contains lower case English alphabets

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def stringComparsion(self, s1, s2):

        # code here

Solutions:
class Solution:

	def stringComparsion(self, s1, s2):
		for i in range(min(len(s1), len(s2))):
			if s1[i] > s2[i]:
				return 1
			elif s1[i] < s2[i]:
				return -1
			if s1[i] == s2[i]:
				if s1[i] == 'n':
					if i + 1 < len(s1) and s1[i + 1] == 'g' and (i + 1 >= len(s2) or s2[i + 1] != 'g'):
						return 1
					elif i + 1 < len(s2) and s2[i + 1] == 'g' and (i + 1 >= len(s1) or s1[i + 1] != 'g'):
						return -1
		if len(s1) > len(s2):
			return 1
		elif len(s2) > len(s1):
			return -1
		return 0


class Solution:

	def stringComparsion(self, string1, string2):
		index = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'ng': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
		l1 = len(string1)
		l2 = len(string2)
		if string1 == string2:
			return 0
		for (i, j) in zip(range(0, l1), range(0, l2)):
			if string1[i] == 'n':
				if i + 1 < l1 and string1[i + 1] == 'g':
					s1 = 'ng'
					i += 1
				else:
					s1 = 'n'
			else:
				s1 = string1[i]
			if string2[j] == 'n':
				if i + 1 < l2 and string2[j + 1] == 'g':
					s2 = 'ng'
					j += 1
				else:
					s2 = 'n'
			else:
				s2 = string2[j]
			if index[s1] > index[s2]:
				return 1
			elif index[s1] < index[s2]:
				return -1
		if l1 > l2:
			return 1
		elif l1 < l2:
			return -1
		else:
			return 0


class Solution:

	def stringComparsion(self, s1, s2):
		if s1 == s2:
			return 0
		i = 0
		j = 0
		while i < len(s1) and j < len(s2):
			if ord(s1[i]) < ord(s2[j]):
				return -1
			if ord(s1[i]) > ord(s2[j]):
				return 1
			if i < len(s1) - 1 and j < len(s2) - 1 and (s1[i] == 'n') and (s1[i + 1] == 'g') and (s2[j + 1] != 'g'):
				return 1
			if j < len(s2) - 1 and i < len(s1) - 1 and (s2[j] == 'n') and (s2[j + 1] == 'g') and (s1[i + 1] != 'g'):
				return -1
			i += 1
			j += 1
		if j == len(s2):
			return 1
		if i == len(s1):
			return -1


class Solution:

	def stringComparsion(self, s1, s2):

		def helper(x):
			ans = []
			if len(x) == 1:
				ans.append(x[0])
			else:
				i = 0
				while i < len(x):
					if x[i:i + 2] == 'ng':
						ans.append('ng')
						i += 2
					else:
						ans.append(x[i])
						i += 1
				if i != len(x):
					ans.append(x[-1])
			return ans
		t = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,ng,o,p,q,r,s,t,u,v,w,x,y,z'
		d = {}
		z = t.split(',')
		for i in range(len(z)):
			d[z[i]] = i
		if s1 == s2:
			return 0
		a = helper(s1)
		b = helper(s2)
		n = len(a)
		m = len(b)
		for i in range(min(n, m)):
			t1 = a[i]
			t2 = b[i]
			if d[t1] < d[t2]:
				return -1
			if d[t1] > d[t2]:
				return 1
		if n > m:
			return 1
		else:
			return -1