Swap letters in a string

前端 未结 4 802
我寻月下人不归
我寻月下人不归 2021-01-12 12:59

I need to swap letters in a string with the following rules:

  • A is replaced by T
  • T is replaced by A
  • C is replaced by G
  • G is replaced
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-12 13:50

    Searching for java "A to T, T to A" found this suggestion:

    String sequence = "AATTTCTCGGTTTCAAT";
    sequence = sequence.replace("A", "t")
                       .replace("T", "a")
                       .replace("C", "g")
                       .replace("G", "c")
                       .toUpperCase();
    System.out.println(sequence);
    

    This is a simple and concise solution that works for your specific situation and will have acceptable performance if your DNA strings are relatively short. For a more general solution for handling large amounts of data you should iterate over the characters one by one and build a new string. Or as polygenelubricants pointed out - consider a storage format that only uses 2 bits per base instead of 16.

提交回复
热议问题