Skip to main content

Data Structures - Strings

1) Find if a given First String is a SubSequence of a Second String


2) Find Max Occurring Frequency Character in  a String


3) Check if Sequence of Characters of Two Strings can become same by removal of a Character


4) Remove all Duplicate Words from a String


5) Find for a Given Two Strings, if one is the rotation of the Other.


6) Find if two Strings are K-Anagrams


7) Longest Common Subsequence where character occurs at least k times.


8) Find if the two given Strings are anagrams of Each Other.

/**
*
*/
package com.datastructures.string;
/**
* @author kkanaparthi
*
* This class is used to find if two
* strings are anagrams of each other
*
* It follows the approach of creating an array to
* store all the ASCII characters, and performs scan of
* the elements in the given two strings one after the
* other, first by adding the characters and second pass
* by removing the characters, after both passes if any
* character is remaining the strings are not anagrams.
*
* The logic is based on the fact that characters and integers
* are replaceable for each other in java
*
*/
public class Anagrams {
/**
* This method finds if the given two strings are
* anagrams of each other
*
* @return
*/
private boolean areAnagrams(String s1,String s2) {
boolean areAnagrams = false;
int[] asciiChars = new int[1<<8];//256
if(s1!=null && s1.length()>0 &&
s2!=null && s2.length()>0) {
if(s1.length()==s2.length()) {
for(char s1Elem : s1.toCharArray()) {
asciiChars[s1Elem]++;
}
for(char s2Elem : s2.toCharArray()) {
asciiChars[s2Elem]--;
}
for(int asciiCharsElem : asciiChars) {
if(asciiCharsElem!=0) {
areAnagrams = false;
} else {
areAnagrams = true;
}
}
}
}
return areAnagrams;
}
/**
* @param args
*/
public static void main(String[] args) {
Anagrams anagrams = new Anagrams();
String s1 = "12345";
String s2 = "54312";
boolean areAnagrams = anagrams.areAnagrams(s1, s2);
System.out.println(" Are Anagrams Value is "+areAnagrams);
}
}
view raw Anagrams.java hosted with ❤ by GitHub


9) Anagram SubString Search

Comments

Popular posts from this blog

Running Multiple Operating Systems(Windows and Ubuntu Linux) on the same machine

VMWare Player is a freely downloadable VMWare. Download VMWare player software and install it on your windows OS download an image of the Ubuntu Linux Desktop version called Ubuntu from http://www.ubuntu.com/getubuntu/download that in iso image format. Then download VMWare configuration bundle that contains a list of files, extract those file to some folder like C:\OS\. Then edit the file" os.vmx file and give the path of the .iso image in that file in the line like below. ide1:0.fileName = C:\OS\ubuntu-8.10-desktop-i386.iso" Now open the file os.vmx file using the vmware player, that will open the Ubuntu OS. You will get a list of options in that select the option install Ubuntu without changing your current configuration of the system Now that will start the Ubuntu OS in a window inside your windows OS. Now you have a browser and all the applications inside the Ubuntu OS, you can start working on that. Double click on this window/expand it to show in full screen. To switch ...