Jumbled Search
Given String Array
strArr[]={“POGI”,”OPIG”,”GIPO”,”GGGG”,”FFFF”,”PIOG”,”PIGG”};You have to find all Strings that
strArr[]={“POGI”,”OPIG”,”GIPO”,”GGGG”,”FFFF”,”PIOG”,”PIGG”};You have to find all Strings that
matches GOPI,at any order. It should give all the Strings that contain G,O,P,ISolution:
JumbledSearch.java
JumbledSearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| public class JumbledSearch { public static void main(String[] args) { String str1="GOPI"; String strArr[]={"POGI","OPIG","GIPO","GGGG","FFFF","PIOG","PIGG"}; for(int i=0;i<strArr.length;i++) { String str2=strArr[i];//1st iteration PIGO if(str1.length()==str2.length())//check only when length is equal { int flag=0; for(int j=0;j<str2.length();j++) { String currChar=String.valueOf(str1.charAt(j));//1st iteration P //We should make sure,Characters are not repeated again int fpos=str2.indexOf(currChar); int lpos=str2.lastIndexOf(currChar); //System.out.println(fpos+"::"+lpos); if(fpos==lpos && fpos!=-1)flag++; } if(flag==str2.length()) System.out.print(str2); } } }} |
No comments:
Post a Comment