Google Interview Question
Software Engineer in Testsimport java.util.ArrayList;
import java.util.HashMap;
public class nonRepeatingExpression {
private String src;
private HashMap<Character,Integer> myMap = new HashMap<Character,Integer>();
private char [] charArr;
public nonRepeatingExpression(String src) {
// TODO Auto-generated constructor stub
this.src = src;
}
private void constructHashMap(){
if(src == null){
System.out.println("Invalid String");
return;
}
charArr = src.toCharArray();
for(int i = 0; i < src.length()-1; i++){
Integer count = myMap.get(charArr[i]);
if(count == null){
myMap.put(charArr[i],1);
}
else{
myMap.put(charArr[i],count+1);
}
}
}
private void findUnique(){
ArrayList<Integer> valueList = new ArrayList<Integer>(myMap.values());
ArrayList<Character> keyList = new ArrayList<Character>(myMap.keySet());
for(int i = 0; i < valueList.size();i++ ){
if(valueList.get(i) == 1){
System.out.println(keyList.get(i));
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
nonRepeatingExpression obj = new nonRepeatingExpression("jkkjFoohkkh");
obj.constructHashMap();
obj.findUnique();
}
}
I also have another solution if the pattern Foo is given before hand and we have a source string in which we shud find if the pattern is unique.
package google;
import java.util.HashMap;
public class FindUniqueRegExp {
private String src;
private String pattern;
private HashMap<String, Integer> myMap = new HashMap<String, Integer>();
public FindUniqueRegExp(String src, String pattern) {
// TODO Auto-generated constructor stub
if (src != null && pattern != null) {
this.src = src;
this.pattern = pattern;
}
}
private void createHashMap() {
int index = 0;
if (src == null) {
System.out.println("Invalid Source");
return;
}
if (pattern == null) {
System.out.println("Invalid Pattern");
return;
}
while ((index + pattern.length()) <= (src.length() - 1)) {
String key = (String) src
.substring(index, index + pattern.length());
Integer count = myMap.get(key);
if (count == null) {
myMap.put(key, 1);
// index++;
} else
myMap.put(key, count + 1);
index++;
}
}
private void isUnique() {
if (pattern != null) {
int value = myMap.get(pattern);
if (value == 1) {
System.out.println("Pattern " + pattern + " is unique!");
} else {
System.out.println("Pattern " + pattern + " is not unique!");
}
}
}
public void findIfUnique() {
createHashMap();
isUnique();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FindUniqueRegExp obj = new FindUniqueRegExp("jhkhFookjjk", "Foo");
obj.findIfUnique();
}
}
<pre lang="" line="1" title="CodeMonkey49564" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}
</pre><pre title="CodeMonkey49564" input="yes">
</pre>
Laksmi and nanmaga - what kind of answers are you giving - they want a regular Expression NOT CODE!!!
- DeziDude October 11, 2008Regular expression to detect occurence of 'foo' in the string is - (.*)(foo)(.*)
Regular expression to detect of 'foo' exactly once in the string is - (.*)(foo)(.*)(fo[^o])(.*)