Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap letters in a string

Tags:

java

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 by C

For example: ACGTA should become TGCAT

What would be the best way to resolve this?

like image 795
Mac135 Avatar asked Jul 15 '10 09:07

Mac135


People also ask

Can we swap characters in a string?

As we know that Object of String in Java are immutable (i.e. we cannot perform any changes once its created). To do modifications on string stored in a String object, we copy it to a character array, StringBuffer, etc and do modifications on the copy object.

How do you swap strings?

For swapping two strings from one location to another location, we use strcpy() function. An array of characters (or) collection of characters is called a string.

How do you swap two elements in a string?

Method #2 : Using join() + replace() + split() The combination of above methods can also be used to perform this task. In this, the task of replace is same, but we use join() and split() with same parameter to perform task of list comprehension.

How do you replace letters in a string in Java?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name.


1 Answers

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.

like image 134
Mark Byers Avatar answered Sep 23 '22 19:09

Mark Byers