经典笔试题 ——判断两个字符串是否为变形词
来源:互联网 发布:淘宝宝贝发布物流 编辑:程序博客网 时间:2024/06/06 05:24
判断两个字符串是否为变形词
【题目】给定两个字符串s1,s2,如果s1和s2中出现的字符种类和字符次数一样多,则s1和s2互为变形词,实现函数判断两个字符串是否为变形次。
【例如】s1=”123”,s2=”321” ,return true;
s1=”123”,s2=”2113”,return false;
分析:判断两个字符串长度是否相等,不等返回错,
相等,先进行排序,当排好序时,进行一个一个对比比较,若不同则返回错,相同返回真为变形词
#include<iostream> 2 using namespace std; 3 4 #include<string> 5 #include<string.h> 6 7 //冒泡排序 8 void bubblesort(char a[], int n) 9 { 10 int i, j,flag = 1; 11 int temp; 12 for(i = 1; i<n && flag == 1; i++) 13 { 14 flag = 0; 15 for(j = 0; j<n-1; j++) 16 { 17 if(a[j] > a[j+1]) 18 { 19 flag = 1; 20 temp = a[j]; 21 a[j] = a[j+1]; 22 a[j+1] = temp; 23 } 24 } 25 } 26 }
//比较 64 bool compare(char *s1, char *s2) 65 { 66 for(int i=0; i<strlen(s1); ++i) 67 { 68 if(s1[i] != s2[i]) 69 { 70 cout<<"error! but size equal"<<endl; 71 return false; 72 } 73 } 74 cout<<"success!"<<endl; 75 return true; 76 } 77 int main() 78 { 79 char s1[] = "48aa2fc33fgtra"; 80 char s2[] = "aa3fag2rt84ca3"; 81 82 int i=0,j=0; 83 if(strlen(s1) != strlen(s2)) 84 { 85 cout<<"error! differ size"<<endl; 86 return false; 87 } 88 bubblesort(s1, strlen(s1));
89 bubblesort(s2, strlen(s2)); 90 compare(s1, s2); 91 return 0; 92 }
阅读全文
0 0
- 经典笔试题 ——判断两个字符串是否为变形词
- 经典笔试题—— 判断两个字符串是否为旋转词
- 字符串问题---判断两个字符串是否为变形词
- 判断两个字符串是否互为变形词
- 判断两个字符串是否互为变形词
- 判断两个字符串是否互为变形词
- 判断两个字符串是否互为变形词
- 判断两个字符串是否互为变形词
- 判断两个字符串是否互为变形词
- 判断两个字符串是否互为变形词
- 判断两个字符串是否互为变形词
- 两个字符串是否为变形词
- 判断字符串是否为变形词
- C++判断字符串是否为变形词
- 判断两个字符串是否为旋转词
- 判断两个字符串是否为兄弟字符串
- 判断两个字符串是否为兄弟字符串
- 判断是否为变形词的分析
- 通过DNS AAAA记录和IPv6地址传输后门有效载荷
- Elven Postman HDU
- Redis伪集群
- 割接—— 业务转移
- tensorflow问题集合
- 经典笔试题 ——判断两个字符串是否为变形词
- Java虚拟机的JVM垃圾回收机制
- 根据当前日期得到前/后n天日期 根据指定日期得到前/后n天日期
- hdoj-1096
- Myeclipse出现Could not create the view: An unexpected exception was thrown.的问题
- 缩放动画源码分析
- 你也可以找到好工作(一)
- EasyTouch
- JS 类型判断 的3种方式