0

Can someone help me?!

With the input abcfbc abfcab it is supposed to give me 4(exludes the redudant characters) but it gives 9 import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int max=1000; System.out.println("enter a string"); Scanner sc=new Scanner(System.in); String string1=sc.nextLine(); if(string1.length()>max) string1=string1.substring(0,max); String []string2=string1.split(" ",2); String sequence1=string2[0]; String sequence2=string2[1]; String sequence3=""; int sum=0; char [] red=new char[1]; for(int i=0;i<sequence1.length();i++) { for(int j=0;j<sequence2.length();j++) { red[0]=sequence2.charAt(j); if(sequence1.charAt(i)==red[0]&&sequence3.contains(red.toString())==false) sequence3=sequence3+red[0]; } } System.out.println(sequence3.length()); } }

2nd May 2020, 12:59 PM
David Bukedi Diela
David Bukedi Diela - avatar
3 Respuestas
+ 1
Michal that's amazing , thank you ! I'll take your code as an example and try to make it shorter
3rd May 2020, 7:08 AM
David Bukedi Diela
David Bukedi Diela - avatar
+ 1
Xerxès no problem and good luck :)
3rd May 2020, 8:35 AM
Michal
Michal - avatar
0
I refactored your code just a little bit. Extra, my proposition https://code.sololearn.com/cqrK50WamPvL/?ref=app
2nd May 2020, 11:42 PM
Michal
Michal - avatar