* вопрос (String размещени) от interviewstreet.com *(String Перестановка) от interviewstreet.com
Предоставлено две строки, написать метод, чтобы решить, если одна перестановка из другой. В вашем решении должна учитываться чувствительность к регистру и .
Перестановка множества объектов - это расположение этих объектов в определенном порядке. Например, существует шесть перестановок строки «abc», а именно «abc», «acb», «bac», «bca», «cab» и «cba».
Выход: Возврат 1, если две строки являются перестановками друг друга. Возврат 0, если две строки не являются перестановками друг друга.
- а ACB кабина сЬ Ьас BCA
импорт java.io. *;
импорт java.util. *;
общественного класс Решения {
частных Set перестановки;
public static void main(String args[]) throws Exception {
// Scanner sc = new Scanner(System.in);
//String string1 = sc.nextLine();
//String string2 = sc.nextLine();
String string1 = "str";
String string2 = "str";
Solution solution = new Solution();
int output = solution.permutation(string1, string2);
System.out.println(output);
}
public void stringPermuation(String s1, String s2) {
if (s2.length() > 0) {
for (int i = 0; i < s2.length(); i++) {
System.out.println(s1 + s2.charAt(i)+","+ s2.substring(0, i)+" +"+ s2.substring(i + 1));
stringPermuation(s1 + s2.charAt(i),
s2.substring(0, i) + s2.substring(i + 1));
}}
else{
permutations.add(s1);
System.out.println(s1);
}
}
public Set stringPermuation(String s) {
permutations = new HashSet<String>();
stringPermuation("", s);
return permutations;
}
private int permutation(String string1, String string2) {
int result = 0;
Set<String> setString1 = stringPermuation(string1);
Set<String> setString2 = stringPermuation(string2);
// create an iterator
System.out.println("There are total of " + setString1.size() + " permutations in String1:");
System.out.println("There are total of " + setString2.size() + " permutations in String2:");
if(setString1.size() == setString2.size())
result=IterateSet(setString1,setString2);
//Return 1 if string1 is a permutation of string2
//Return 0 if string1 is not a permutation of string2
return result;
}
public int IterateSet(Set setString1,Set setString2){
int i= 0;
Iterator<String> it = setString1.iterator();
while (it.hasNext()) {
if(setString2.contains(it.next()) && i == 0)
i=1;
}
return i;}}
Это вопрос, который здесь скрывается? – Eran