You are given two strings and that consist of lower case Latin letters and both strings contain single '?'. You need to replace '?' with some lower case Latin letter in both strings not necessarily the same.
After that, you can apply the following operation on any number of times:
- Swap any two adjacent characters.
Your task is to determine if it is possible to make equal to if you replace '?' with a lower case Latin letter and apply the mentioned operation on any number of times.
Example:
Input: str1="abc?", str2="abc?",n=4
Output: YES
Approach
Java
public class ReplaceStrings {public static void main(String args[]){String str1="abc?";String str2="abc?";int n=4;char a[]=str1.toCharArray();char b[]=str2.toCharArray();int i,c[][]=new int[2][26];for(i=0;i<n;i++){if(a[i]!='?') c[0][a[i]-'a']++;if(b[i]!='?') c[1][b[i]-'a']++;}int d=0;for(i=0;i<26;i++)d+=Math.abs(c[0][i]-c[1][i]);if(d<3) System.out.println("YES");else System.out.println("NO");;}}
No comments:
Post a Comment