Beautiful Strings
Problem Statement :
You are given a string, S, consisting of lowercase English letters. A string is beautiful with respect to S if it can be derived from S by removing exactly 2 characters. Find and print the number of different strings that are beautiful with respect to S. Input Format A single string of lowercase English letters denoting S. Constraints 3 <= |S| <= 10^6 3 <= |S| <= 20 holds for test cases worth at least 15% of the problem's score. 3 <= |S| <= 2000 holds for test cases worth at least of the problem's score. Output Format Print the number of different strings that are beautiful with respect to S.
Solution :
Solution in C :
In C++ :
/*
*/
//#pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <assert.h>
#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define eps 1e-9
#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512
const int N = 1031;
using namespace std;
string st;
int naive(string st)
{
set<string> res;
for (int i = 0; i < st.size(); i++)
{
for (int j = i + 1; j < st.size(); j++)
{
string temp = "";
for (int q = 0; q < st.size(); q++)
{
if (q != i&&q != j)
temp += st[q];
}
res.insert(temp);
}
}
set<string>::iterator it;
/*for (it = res.begin(); it != res.end(); it++)
{
cout << *it << endl;
}*/
return res.size();
}
long long smart(string st)
{
int cnt = 0;
vector<int> v;
long long ans = 0;
for (int i = 0; i < st.size(); i++)
{
if (i == 0 || st[i] == st[i - 1])
++cnt;
else
{
v.push_back(cnt);
cnt = 1;
}
}
v.push_back(cnt);
for (int i = 0; i < v.size(); i++)
{
if (v[i]>1)
++ans;
}
for (int i = 1; i +1< st.size(); i++)
{
if (st[i - 1] == st[i + 1] && st[i] != st[i - 1])
--ans;
}
ans += v.size() * 1ll * (v.size() - 1) / 2;
return ans;
}
int main(){
// freopen("hospital.in","r",stdin);
// freopen("hospital.out","w",stdout);
//freopen("F:/in.txt", "r", stdin);
//freopen("F:/output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
//cin.tie(0);
cin >> st;
// cout << naive(st) << endl;
cout << smart(st) << endl;
cin.get(); cin.get();
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class CharGroup {
public char c;
public int n;
public long [] removeCount;
CharGroup(char ch) {
c = ch;
n = 1;
removeCount = new long[3];
}
}
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
CharGroup last = null;
ArrayList<CharGroup> g = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
char nc = s.charAt(i);
if ((i == 0) || (last.c != nc)) {
last = new CharGroup(nc);
g.add(last);
} else {
last.n++;
}
}
for (int i = 0; i < g.size(); i++){
CharGroup cg = g.get(i);
cg.removeCount[1] = i+1;
if (i > 0) {
cg.removeCount[2] =
g.get(i-1).removeCount[2]+
g.get(i-1).removeCount[1];
}
if (cg.n >= 2) {
cg.removeCount[2]++;
}
if ((i>=2)&&(g.get(i-1).n==1)&&
(cg.c == g.get(i-2).c)) {
cg.removeCount[2]--;
}
}
System.out.printf("%d\n", g.get(g.size()-1).removeCount[2]);
}
}
In C :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char str[1000001];
int count[1000000],mark[1000000]={0};
int main(){
int len,cs,i;
long long ans;
scanf("%s",str);
len=strlen(str);
for(i=cs=0;i<len;i++){
if(!i || str[i]!=str[i-1])
count[cs++]=1;
else
count[cs-1]++;
if(i && i<len-1 && str[i]!=str[i-1] && str[i-1]==str[i+1])
mark[cs-1]=1;
}
for(i=ans=0;i<cs;i++){
ans+=i;
if(count[i]>1)
ans++;
if(mark[i])
ans--;
}
printf("%lld",ans);
return 0;
}
In Python3 :
import operator as op
import functools
def ncr(n, r):
if n < r: return 0
r = min(r, n-r)
if r == 0: return 1
numer = functools.reduce(op.mul, range(n, n-r, -1))
denom = functools.reduce(op.mul, range(1, r+1))
return numer//denom
s = input()
s += ' '
n_blocks = 0
block_size = 0
count = 0
prev = None
prev2 = None
for c in s:
if prev and c != prev:
if block_size > 1:
count += 1
n_blocks += 1
block_size = 1
if c == prev2:
count -= 1
else:
block_size += 1
prev2 = prev
prev = c
print(ncr(n_blocks, 2) + count)
View More Similar Problems
Tree: Huffman Decoding
Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t
View Solution →Binary Search Tree : Lowest Common Ancestor
You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b
View Solution →Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →Square-Ten Tree
The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the
View Solution →