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.
9) Anagram SubString Search
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
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); | |
} | |
} |
9) Anagram SubString Search
Comments
Post a Comment