简体   繁体   中英

Transform a String to a String based on a condition

As input, I have a String Object that contains only '0'.

Example of the input String word = "00000" .

The question is to transform this String to another String as an output.

The transformation is to replace some '0' by '1' in some specific positions that results on every '0' is adjacent to at least one '1'.

some examples:

input     |    output
0000          0101
00000         01001
000000        010010
0000000       0100101
00000000      01001010

As we can see, the outputs fulfill the condition, every '0' is adjacent to at least one '1'.

The second condition is that we must use the min number of '1'.

After some work I realized that we can know the min number of '1' that should replace the 0 values.

The formula is represented by the getMinNumber(String word) method.

Here is what i have done.

  public class TestUtilities {

    public static void main( String[] args ) {

        String [] words ={"0000","00000","000000","0000000"} ;
        for (String str :words) {
            System.out.println(transform(str));
        }
    }
    private static String  transform( String word ) {
        int min =  getMinNumber(word);
        //////
        //Some processing Here  
        /////
        return "";
    }

    public static int getMinNumber(String word) {
        int min;
        if (word.length() % 3 == 0) {
            min = word.length() / 3;
        } else {
            min = (word.length() / 3) + 1;
        }
        return min;
    }
}

As shown i left the processing part because i coudn't find the fittest algorithm. Normally i should replace one 0 every three 0, but it dosen't work for every word.

I'm looking for a solution that can process any word.

To get minimal amounts of 1 we need each 1 to handle as many 0 as possible which means we need to repeat 010 maximal amount of times. So we can start replacing zeroes with 010 from left

0000000000
↓↓↓↓↓↓↓↓↓
010010010x   <- here you can't place another 010 and we have one extra place to fill

If 010 didn't fill entire number this means we need either one or two more digits.

What should we do if it is one digit? Can we use 0 here? Lets see: 010010010x would become 0100100100 ?
Is it valid result? NO because last zero doesn't have any 1 near it. So we can't use 0 which leaves us with 1 like 0100100101 .

So if we need one more digit we replace it with 1 .

Now what happens when we have two digits to fill, like 010010010xx ? We can't use 00 because last 0 wouldn't have any adjacent 1 .

  • Can we use 01 ? YES: because we will get 010010010 all 0 have adjacent 1 所有0都具有相邻的1
  • Can we use 10 ? YES: because we will get 010010010 and here also all 0 have adjacent 1 ,这里所有0都具有相邻的1

Should we check 11? No because we already can use 01 or 10 which add one 1 while 11 adds two 1 so that result wouldn't contain minimal amount of 1 .


I will leave writing code using that approach to you.

I find @Pshemo's answer elegant and straight forward. With his approach you even don't need to calculate the minimum counts of 1 's to replace. Nevertheless I have an alternative way which may be helpful:

public static void main( String[] args ) {

    String [] words ={"00","000","0000","00000","000000","0000000","00000000","000000000","0000000000"} ;
    for (String str :words) {
        System.out.println(transform(str));
    }
}
private static String  transform( String word ) {
    int min =  getMinNumber(word);
    StringBuilder sb = new StringBuilder(word);
    //starting from char at index 1 replace each third '0' with '1'
    //and substract 1 for each replaced char from min 
    for(int i = 1; i< word.length(); i = i+3){
        sb.setCharAt(i, '1');
        min--;
    }
    //if minimum replacement count not yet met replace last char
    if(min >0){
        sb.setCharAt(word.length()-1, '1');
    }
    return sb.toString();
}

public static int getMinNumber(String word) { 
    //just replaced your logic to make it shorter; you can keep your own implementation
    return (int) Math.ceil(word.length() / 3.);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM