2016-10-01 39 views
0

我需要做一个方法来检查两个字符串例如bod和bot或螃蟹和rab。该方法需要打印出用户为了使它们平等而必须做的事情。例如在bod和bot中,它会打印出“替换,2,d在字符串中”。我用这个代码似乎工作。方法做比较和更改字符串中的单个字符

if(a.length()==b.length()){ 
     int i; 
     for(i=0; i<=a.length(); i++){ 
      if(a.charAt(i)!=b.charAt(i)){ 
       return "replace,"+ i + "," + b.charAt(i); 
       } 
      } 
     } 

,但我有麻烦,如果两个字符串不相等的大小。我使用这个,但它不工作,因为其中一个字符串更大。

int aS = a.length(); 
    int bS = b.length(); 
    if(bS - aS == 1){ 
     int i; 
     for(i=0; i<=b.length(); i++){ 
      if(b.charAt(i)!=a.charAt(i)){ 
       return "remove," + i; 
      } 
     } 
    } 

你们可以给我一个建议什么方法我可以用它来检查哪些是多余的字母或反之亦然信我可以添加,然后返回一个字符串说要么删除字符或添加一个额外的一个。谢谢

+0

字符串只有一个字符不同吗? – Arthas

+0

是的,它所有的字符串将相差一个字符 – Bakslat

+0

在我看来,删除方法应该工作,除非你冒险超越较短的字符串,例如汽车和购物车的界限。所以你应该只比较较短字符串的长度(独占),如果它们相等,则删除较长字符串的最后一个字符。希望能帮助到你。 –

回答

0

也许是这样的?

public ArrayList<String> createConversionList(String primary, String secondary){ 
    //Determine which string is shorter. 
    String shorter; 
    String longer; 
    boolean primaryIsShorter = false; 
    if (primary.length() >= secondary.length()){ 
    longer = primary; 
    shorter = secondary; 
    } else{ 
    longer = secondary; 
    shorter = primary; 
    primaryIsShorter = true; 
    } 

    //Fills an array with all the character positions that differ between the 
    //two strings, using the shorter string as the base. 
    int[] posOfCharsToChange = new int[shorter.length()]; 
    for(int i = 0; i < shorter.length(); i++){ 
    if(shorter.charAt(i) != longer.charAt(i)){ 
     posOfCharsToChange[i] = i; 
    } else{ 
     posOfCharsToChange[i] = -1; 
    } 
    } 

    //Adds to an ArrayList all of the "Replace" strings. 
    ArrayList<String> conversionList = new ArrayList(); 
    for(int pos: posOfCharsToChange){ 
    if(pos != -1){ 
     String s = "Replace " + secondary.charAt(pos) + " with " + primary.charAt(pos) + ". \n"; 
     conversionList.add(s); 
    } 
    } 

    //Depending on which string was bigger, either adds "Add" or "Remove" 
    //strings to the ArrayList. If the strings were the same size, does 
    //nothing. 
    if(primary.length() != secondary.length()){ 
    if(primaryIsShorter){ 
     for(int i = primary.length(); i < secondary.length(); i++){ 
     String s = "Remove " + secondary.charAt(i) + ". \n"; 
     conversionList.add(s); 
     } 
    } 
    else{ 
     for(int i = secondary.length(); i < primary.length(); i++){ 
     String s = "Add " + primary.charAt(i) + ". \n"; 
     conversionList.add(s); 
     } 
    } 
    } 

    return conversionList; 
} 
0

我的方法的工作原理如下

1)我们以较小的字符串,并把它的所有内容在一个ArrayList

2)我们以更大的字符串,并把它的内容ArrayList中只有其不存在于数组列表

3)在ArrayList中的最后一个字符必须从更大的串被移除以使它们相等

实施例1: 一个= RAB b =蟹

1)的ArrayList = RAB - 一个加入

2)的ArrayList = RAB的>内容Ç - >只有B的独特内容被添加

出2: 一个=蟹 b = RAB

1)的ArrayList = RAB

2)的ArrayList = RABC

类似地,如果位置是在中间或根本不启动,

例如:A = racb B = RAB

1)的ArrayList = RAB

2)的ArrayList = RAB Ç

public class Replace { 

public static void main(String args[]) { 
    int p = 0, j = 0; 
    String a = "rab"; 
    String b = "crab"; 

    if (b.length() < a.length()) { 

     ArrayList al = new ArrayList(); 

     for (j = 0; j < b.length(); j++) { 
      if (!al.contains(b.charAt(j))) { 
       al.add(b.charAt(j)); 
      } 

     } 

     for (j = 0; j < a.length(); j++) { 
      if (!al.contains(a.charAt(j))) { 
       al.add(a.charAt(j)); 
      } 

     } 

     System.out.println("Remove " + al.get(al.size() - 1) 
       + " from String a"); 
    } else { 

     ArrayList al = new ArrayList(); 

     for (j = 0; j < a.length(); j++) { 
      if (!al.contains(a.charAt(j))) { 
       al.add(a.charAt(j)); 
      } 

     } 

     for (j = 0; j < b.length(); j++) { 
      if (!al.contains(b.charAt(j))) { 
       al.add(b.charAt(j)); 
      } 

     } 

     System.out.println("Remove " + al.get(al.size() - 1) 
       + " from String b"); 

    } 

    } 

} 

注 - 该程序只适用于您给定的限制条件,即字符串仅在一个字符上有所不同,并且如果我们删除或添加两个字符串的顺序并不不同那个专家。