Meta String geeks solution java
Problem: Meta strings are the strings which can be made equal by exactly one swap in any of the strings.
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static int check(String str,String s)
{
int count=0;
boolean flag=false;
int index=-1;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)!=s.charAt(i))
{
if(!flag){
index=i;
count++;
flag=true;
}
else if(count<2)
{
if(str.charAt(i)!=s.charAt(index))
return 0;
flag=false;
}
}
}
return (count==1)?1:0;
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
String str=ab.next();
String str2=ab.next();
if(str.length()!=str2.length())
System.out.println("0");
else
{
System.out.println(check(str,str2));
}
}
}
}
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static int check(String str,String s)
{
int count=0;
boolean flag=false;
int index=-1;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)!=s.charAt(i))
{
if(!flag){
index=i;
count++;
flag=true;
}
else if(count<2)
{
if(str.charAt(i)!=s.charAt(index))
return 0;
flag=false;
}
}
}
return (count==1)?1:0;
}
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
String str=ab.next();
String str2=ab.next();
if(str.length()!=str2.length())
System.out.println("0");
else
{
System.out.println(check(str,str2));
}
}
}
}
0 Comments:
Post a Comment