Skip to content

Commit

Permalink
string
Browse files Browse the repository at this point in the history
  • Loading branch information
RryLee committed Feb 29, 2016
1 parent b528626 commit 88d2bb3
Show file tree
Hide file tree
Showing 24 changed files with 697 additions and 0 deletions.
31 changes: 31 additions & 0 deletions string/1-pangrams.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
char small[26] = {0};
char big[26] = {0};
int i, n;
char s[1000];
while (scanf("%s", &s[strlen(s)]) == 1);
n = strlen(s);
for (int i = 0; i < n; i++) {
if (s[i] >= 'a' && s[i] <= 'z') {
small[s[i] - 'a'] = 1;
}
else if (s[i] >= 'A' && s[i] <= 'Z') {
big[s[i] - 'A'] = 1;
}
}

for (int i = 0; i < 26; i++) {
if (!(big[i] == 1 || small[i] == 1)) {
printf("not pangram");
return 0;
}
}

printf("pangram");

return 0;
}
17 changes: 17 additions & 0 deletions string/1-pangrams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$handle = fopen("php://stdin", "r");
$string = fgets($handle);

$string = strtolower($string);

$exists = true;

foreach (range('a', 'z') as $c) {
if (strpos($string, $c) === false) {
$exists = false;
break;
}
}

echo ($exists ? "pangram" : "not pangram") . PHP_EOL;
42 changes: 42 additions & 0 deletions string/10-palindrome-index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $T);

while ($T --) {
fscanf($handle, "%s", $str);

$start = 0;
$end = strlen($str) - 1;

while ($start < $end) {
if ($str[$start] == $str[$end]) {
$start ++;
$end --;
} else {
break;
}
}

if ($start >= $end) {
echo "-1\n";
continue;
}

$left = true;
$start_temp = $start;
$end_temp = $end;
$start_temp ++;
while ($start_temp < $end_temp) {
if ($str[$start_temp] == $str[$end_temp]) {
$start_temp ++;
$end_temp --;
} else {
$left = false;
break;
}
}

echo $left ? $start : $end;
echo "\n";
}
5 changes: 5 additions & 0 deletions string/11-reverse-shuffle-merge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%s", $S);

21 changes: 21 additions & 0 deletions string/12-the-love-letter-mystery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<bits/stdc++.h>

using namespace std;

int main()
{
int t,i;
cin>>t;
while(t--)
{
string str;
cin>>str;
int s = 0;
for(i=0 ; i< str.length()/2 ; i++)
{
s += abs(str[i] - str[str.length()-i-1]);
}
cout<<s<<endl;
}
return 0;
}
26 changes: 26 additions & 0 deletions string/12-the-love-letter-mystery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $T);

while ($T --) {
fscanf($handle, "%s", $S);

$length = strlen($S);
$start = 0;
$end = $length - 1;
$count = 0;
while ($start < $end) {
if ($S[$start] == $S[$end]) {
$start ++;
$end --;
continue;
} else {
$count += abs(ord($S[$start]) - ord($S{$end}));
$start ++;
$end --;
}
}

echo $count . PHP_EOL;
}
Binary file added string/13-common-child
Binary file not shown.
36 changes: 36 additions & 0 deletions string/13-common-child.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{
string s1, s2;
cin >> s1 >> s2;
int length = s1.length();
int **array = new int*[length + 1];
for (int i = 0; i < length + 1; ++i)
{
array[i] = new int[length + 1];
}

for (int i = 0; i < length; ++i)
{
for (int j = 0; j < length; ++j)
{
if (s1[i] == s2[j])
{
array[i + 1][j + 1] = array[i][j] + 1;
}
else
{
array[i + 1][j + 1] = max(array[i + 1][j], array[i][j + 1]);
}
}
}

cout << array[length][length] << endl;

return 0;
}
35 changes: 35 additions & 0 deletions string/2-funny-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main(int argc, char const *argv[])
{
string s, r;
int times;
cin >> times;

for (int i = 0; i < times; ++i)
{
cin >> s;
int len = s.length();

bool funny = true;
for (int j = 1; j < len; ++j)
{
if (abs(s[len - j] - s[len - j - 1]) != abs(s[j] - s[j - 1])) {
funny = false;
break;
}
}

if (funny) {
cout << "Funny" << endl;
} else {
cout << "Not Funny" << endl;
}
}

return 0;
}
20 changes: 20 additions & 0 deletions string/2-funny-string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $T);

for ($i=0; $i < $T; $i++) {
$s = fgets($handle);
$s = trim($s);
$r = strrev($s);

$funny = true;
for ($j=1; $j < strlen($s); $j++) {
if (abs(ord($s[$j]) - ord($s[$j - 1])) != abs(ord($r[$j]) - ord($r[$j - 1]))) {
$funny = false;
break;
}
}

echo $funny ? "Funny\n" : "Not Funny\n";
}
27 changes: 27 additions & 0 deletions string/3-alternating-characters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char const *argv[])
{
int T;
cin >> T;

while (T --) {
string str;
cin >> str;

int ans = 0;
for (int i = 0; i < str.length() - 1; ++i)
{
if (str[i] == str[i + 1])
ans ++;
}

cout << ans << endl;
}

return 0;
}
17 changes: 17 additions & 0 deletions string/3-alternating-characters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $T);

for ($i=0; $i < $T; $i++) {
fscanf($handle, "%s", $str);

$count = 0;
for ($j=1; $j < strlen($str); $j++) {
if ($str[$j] == $str[$j - 1]) {
$count ++;
}
}

echo $count . PHP_EOL;
}
37 changes: 37 additions & 0 deletions string/4-game-of-thrones.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char const *argv[])
{
string str;
cin >> str;
int A[26];
for (int i = 0; i < 26; ++i)
{
A[i] = 0;
}
for (int i = 0; i < str.length(); ++i)
{
A[str[i] - 'a'] ++;
}

int sum = 0;
for (int i = 0; i < 26; ++i)
{
sum = sum + (A[i] % 2);
}

if (sum >= 2)
{
cout << "NO" << endl;
}
else
{
cout << "YES" << endl;
}

return 0;
}
43 changes: 43 additions & 0 deletions string/4-game-of-thrones.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%s", $str);

$length = strlen($str);

$flag = true;
$some = [];

for ($i=0; $i < $length; $i++) {
$index = ord($str[$i]) - ord('a');
if (isset($some[$index])) {
$some[$index] ++;
} else {
$some[$index] = 1;
}
}

if ($length % 2 == 0) {
foreach ($some as $a) {
if ($a % 2 !== 0) {
$flag = false;
break;
}
}
} else {
$odd = 0;
$even = 0;
foreach ($some as $a) {
if ($a % 2 !== 0) {
$even ++;
} else {
$odd ++;
}
}

if ($even != 1 || $odd == $length - 1) {
$flag = false;
}
}

echo $flag ? "YES\n" : "NO\n";
Loading

0 comments on commit 88d2bb3

Please sign in to comment.